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
Instances from service factories do not respect lifestyle.
Steps to reproduce
Create a service, register it as "per web request" (MsScopedLifestyleManager) (see SomeService below)
Create a service factory, register it as a factory (see ISomeServiceFactory below)
Create a Controller with dependencies - service and also service factory. Create a service from service factory in a method. (see MainController below)
There should be only one instance of SomeService per web request because of the lifestyle. Parameters in constructor shares one instance, but service factory creates new instances (see comments OK and FAIL).
public class SomeService
{
}
public interface ISomeServiceFactory
{
SomeService CreateService();
}
public class Startup
{
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
var container = new WindsorContainer();
container.AddFacility<TypedFactoryFacility>();
container.Register(Component.For<ISomeServiceFactory>().AsFactory()); // register service factory
container.Register(Component.For<SomeService>().LifeStyle.Custom<MsScopedLifestyleManager>()); // register SomeService as "per-web-request"
return WindsorRegistrationHelper.CreateServiceProvider(container, services);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseMvc();
}
}
[Route("")]
public class MainController : ControllerBase
{
private readonly SomeService someService1;
private readonly SomeService someService2;
private readonly ISomeServiceFactory someServiceFactory;
public MainController(SomeService someService1, SomeService someService2, ISomeServiceFactory someServiceFactory)
{
this.someService1 = someService1;
this.someService2 = someService2;
this.someServiceFactory = someServiceFactory;
}
[HttpGet]
public void Do()
{
var instance1 = someServiceFactory.CreateService();
var instance2 = someServiceFactory.CreateService();
Debug.Assert(Object.ReferenceEquals(someService1, someService2)); // OK
Debug.Assert(Object.ReferenceEquals(instance1, instance2)); // FAIL
Debug.Assert(Object.ReferenceEquals(someService1, instance1)); // FAIL
}
}
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
Dependencies
Castle.Windsor 4.1.1
Castle.Windsor.MsDependencyInjection 3.3.1
Microsoft.AspNetCore.App 2.1.6
The text was updated successfully, but these errors were encountered:
Instances from service factories do not respect lifestyle.
Steps to reproduce
Dependencies
The text was updated successfully, but these errors were encountered: