简体   繁体   English

尝试在Windsor .net Core中注册作用域组件时,为什么会发生“作用域不可用”错误?

[英]Why 'Scope was not available' error happens when trying to register scoped component in Windsor .net core?

I am using Windsor Castle. 我正在使用温莎城堡。 I get 我懂了

System.InvalidOperationException: 'Scope was not available. System.InvalidOperationException:'范围不可用。 Did you forget to call container.BeginScope()?' 您是否忘记了调用container.BeginScope()?

error when I register the .NET standard library in the MVC5 config file and I try to use Scoped lifestyle. 当我在MVC5配置文件中注册.NET标准库并且尝试使用Scoped生活方式时出现错误。 Why am I getting this error? 为什么会出现此错误?

I have an MVC5 project that references a lot of .NET Framework class library projects. 我有一个MVC5项目,它引用了许多.NET Framework类库项目。 All of those class library projects have Windsor Installer classes in order registration to be called based on the MVC5 config file. 所有这些类库项目都具有Windsor Installer类,以便基于MVC5配置文件调用注册。 Library Castle. 图书馆城堡。 Windsor has PerWebRequest lifestyle definition, and it was used in all those installers. 温莎具有PerWebRequest生活方式定义,并且在所有这些安装程序中都使用了它。

Now, I introduced .NET standard class library and created installer class like for all previous projects except that I am using Scoped lifestyle instead of PerWebRequest (I read somewhere that Scoped is replacement for PerWebRequest in .net Core ), I added the registration code to MVC5 config, like for the rest of libraries, and now I get exception: 现在,我介绍了.NET标准类库并为所有以前的项目创建了安装程序类,除了我使用的是Scoped生活方式而不是PerWebRequest (我在NETd Core中读到Scoped代替了PerWebRequest ),然后将注册代码添加到MVC5配置,就像其他库一样,现在我得到了异常:

System.InvalidOperationException: 'Scope was not available. System.InvalidOperationException:'范围不可用。 Did you forget to call container.BeginScope()?' 您是否忘记了调用container.BeginScope()?

I saw the similar question here: Scope was not available. 我在这里看到了类似的问题: 范围不可用。 Did you forget to call container.BeginScope()? 您是否忘了调用container.BeginScope()? but it is not answered and difference is that I am trying to use .NET standard library in MVC5 with .NET Framework. 但是没有得到答案,区别是我试图在带有.NET Framework的MVC5中使用.NET标准库。

This is my installer class: 这是我的安装程序类:

public void Install(Castle.Windsor.IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
{
     container.Register(Component.For(typeof(IAzureSBConfiguration)).ImplementedBy(typeof(ConfigFileConfiguration)).LifestyleScoped());
}

This is config file registration: 这是配置文件注册:

<install assembly="AzureSBQueuing" />

I expected everything to work normal as it worked when introduce .NET standard class library. 我希望一切都能正常工作,因为在引入.NET标准类库时,一切正常。 Why is error happening? 为什么会发生错误?

.LifestyleScoped() is not any replacement for PerWebRequest . .LifestyleScoped()不能替代PerWebRequest If you register service with .LifestyleScoped() , you have to resolve it within scope created with BeginScope method. 如果使用.LifestyleScoped()注册服务, .LifestyleScoped()必须在使用BeginScope方法创建的范围内解决该服务。 But this has nothing to do with AddScoped() from asp.net core framework. 但这与asp.net核心框架的AddScoped()没有关系。

ASP.NET-MVC and ASP.NET-CORE are completely different frameworks and therefore also their Windsor integrations are delivered with different packagages and have different scoping mechanism. ASP.NET-MVC和ASP.NET-CORE是完全不同的框架,因此它们的Windsor集成以不同的包装提供,并且具有不同的作用域机制。

Therefore there is no common registration that you could use within .netstandard library. 因此,.netstandard库中没有可以使用的通用注册。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 温莎城堡的生活方式无法按范围注册 - Castle Windsor scoped life style fails to register per scope 温莎城堡温莎登记册组成部分 - Castle Windsor Register Component by Convention 调用温莎城堡时的.NET Fiddle错误 - .NET fiddle error when invoking Castle Windsor 尝试在 .NET Core 中更新数据库时出错 - Error when trying to update database in .NET Core 尝试在asp.net mvc中注册用户时出错 - Error When trying to Register a User in asp.net mvc 温莎城堡-无法将适当的成分注册到浮石中 - Castle windsor - Unable to register a proper component with argumants Windsor为多个接口注册单例组件 - Windsor register singleton component for multiple interfaces 在 ASP.NET Core 3.0 中发生错误请求错误时显示错误属性 - Display error property when a bad request error happens in ASP.NET Core 3.0 尝试使用来自 .NET 核心 Web 应用程序的 OpenIDConnect 进行身份验证时无效 scope - Invalid scope when trying to authenticate using OpenIDConnect from a .NET Core Web App 在asp.net核心Web API上使用Autofac在同一存储库上发生多个http请求时发生错误 - error when multiple http request happens on the same repository with Autofac on asp.net core web api
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM