2

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?

1 Answer 1

2

.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.

3
  • Jan, great explanation. Which approach should I use in my case which is: main project is ASP.Net-mvc, but Windsor installer class is in the .NET standard library that main project references. Since Castle.Windsor Nuget package version is for .NET core, there is no PerWebRequest lifestyle and also no MsScopedLifestyleManager class?
    – Miloš
    May 5, 2019 at 13:01
  • 1
    @Miloš, the simpliest solution is to retarget your library to .net framework or add multiple target platforms to your library and conditionally compile .LifestylePerWebRequest() for .net framework target. May 5, 2019 at 17:01
  • Jan, library must be .NET standard since it is not referenced just from ASP.NET MVC, but also from a few more projects which are .NET Core.
    – Miloš
    May 6, 2019 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.