[英]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集成以不同的包装提供,并且具有不同的作用域机制。
.LifestylePerWebRequest()
.LifestylePerWebRequest()
.LifestyleCustom<MsScopedLifestyleManager>()
from package - https://github.com/volosoft/castle-windsor-ms-adapter .LifestyleCustom<MsScopedLifestyleManager>()
: //github.com/volosoft/castle-windsor-ms-adapter Therefore there is no common registration that you could use within .netstandard library. 因此,.netstandard库中没有可以使用的通用注册。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.