简体   繁体   中英

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. Did you forget to call container.BeginScope()?'

error when I register the .NET standard library in the MVC5 config file and I try to use Scoped lifestyle. Why am I getting this error?

I have an MVC5 project that references a lot of .NET Framework class library projects. All of those class library projects have Windsor Installer classes in order registration to be called based on the MVC5 config file. Library Castle. Windsor has PerWebRequest lifestyle definition, and it was used in all those installers.

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:

System.InvalidOperationException: 'Scope was not available. Did you forget to call container.BeginScope()?'

I saw the similar question here: Scope was not available. Did you forget to call container.BeginScope()? but it is not answered and difference is that I am trying to use .NET standard library in MVC5 with .NET Framework.

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. Why is error happening?

.LifestyleScoped() is not any replacement for PerWebRequest . If you register service with .LifestyleScoped() , you have to resolve it within scope created with BeginScope method. But this has nothing to do with AddScoped() from asp.net core framework.

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.

Therefore there is no common registration that you could use within .netstandard library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM