You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When a WindsorScopedServiceProvider is created it captures the scope. This has some odd effects, for example when a IServiceProvider is injected into a singleton instance. In this case the captured scope would outlive the lifetime of the scope.
The following sample crashes with the error System.ObjectDisposedException: Scope cache was already disposed. This is most likely a bug in the calling code..
usingCastle.Core;usingCastle.MicroKernel;usingCastle.MicroKernel.Context;usingCastle.Windsor.Extensions.DependencyInjection;usingMicrosoft.Extensions.DependencyInjection;// construct a ServiceProvider with a transient TeapotService and a singleton SingletonTeapotService.varserviceCollection=newServiceCollection();serviceCollection.AddTransient<TeapotService>().AddSingleton<SingletonTeapotService>();varfactory=newWindsorServiceProviderFactory();varbuilder=factory.CreateBuilder(serviceCollection);varserviceProvider=factory.CreateServiceProvider(builder);// call to singleton in scopeusing(varscope=serviceProvider.CreateScope()){// resolving the singleton from the scope causes the WindsorScopedServiceProvider (dependency of// SingletonTeapotService) to capture the current scope instead of the root scope! this means that// the WindsorScopedServiceProvider instance will be in an invalid state after the scope has ended.scope.ServiceProvider.GetRequiredService<SingletonTeapotService>().Say();}// call to singleton in rootscope while its dependency WindsorScopedServiceProvider is in an invalid stateserviceProvider.GetRequiredService<SingletonTeapotService>().Say();// error! ObjectDisposedExceptionclassSingletonTeapotService{privatereadonlyIServiceProvider_serviceProvider;publicSingletonTeapotService(IServiceProviderserviceProvider)=>_serviceProvider=serviceProvider;publicvoidSay()=>_serviceProvider.GetRequiredService<TeapotService>().Say();}classTeapotService:IDisposable{publicTeapotService()=>Console.WriteLine("create teapot");publicvoidSay()=>Console.WriteLine("I'm a teapot!");publicvoidDispose()=>Console.WriteLine("dispose teapot");}
I'd have expected this code to behave in the same way Microsoft.Extensions.DependencyInection would. With MS DI the SingletonTeapotService's IServiceProvider would be scoped to the rootscope.
The text was updated successfully, but these errors were encountered:
When a
WindsorScopedServiceProvider
is created it captures the scope. This has some odd effects, for example when aIServiceProvider
is injected into a singleton instance. In this case the captured scope would outlive the lifetime of the scope.The following sample crashes with the error
System.ObjectDisposedException: Scope cache was already disposed. This is most likely a bug in the calling code.
.I'd have expected this code to behave in the same way Microsoft.Extensions.DependencyInection would. With MS DI the SingletonTeapotService's IServiceProvider would be scoped to the rootscope.
The text was updated successfully, but these errors were encountered: