Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factory services are not supported for MsScopedLifestyleManager #32

Open
jirikanda opened this issue Dec 19, 2018 · 4 comments
Open

Factory services are not supported for MsScopedLifestyleManager #32

jirikanda opened this issue Dec 19, 2018 · 4 comments
Milestone

Comments

@jirikanda
Copy link

Instances from service factories do not respect lifestyle.

Steps to reproduce

  1. Create a service, register it as "per web request" (MsScopedLifestyleManager) (see SomeService below)
  2. Create a service factory, register it as a factory (see ISomeServiceFactory below)
  3. Create a Controller with dependencies - service and also service factory. Create a service from service factory in a method. (see MainController below)
  4. 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
@acjh
Copy link

acjh commented Dec 20, 2018

How did you implement ISomeServiceFactory?

@jirikanda
Copy link
Author

Thanks for the reply.

I do not implement ISomeServiceFactory myself.
I am using Castle Windsor's Typed Factory Facility to let it create the implementation at runtime.

container.AddFacility<TypedFactoryFacility>(); 
container.Register(Component.For<ISomeServiceFactory>().AsFactory());

See https://github.com/castleproject/Windsor/blob/master/docs/typed-factory-facility-interface-based.md

@jirikanda
Copy link
Author

jirikanda commented Feb 15, 2019

I found that #25 partially solves this problem.
Using fix in #25, typed factories work well from the second request.

For the very first request, typed factories still resolve new instances.

@hikalkan hikalkan added this to the v3.4 milestone Feb 20, 2019
@hikalkan
Copy link
Member

I will check this.

@hikalkan hikalkan modified the milestones: v3.4, v3.5 Aug 19, 2020
@ismcagdas ismcagdas modified the milestones: v4.0, v4.2 Nov 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants