0

My application is based on ASP.NET WebAPI and I am using Unity for DI. My ResourceProvider class has a dependency of type string culturename. As culturename would be different in every web request.

How should I register ResourceProvider in UnityConfig to be able to pick new culturename for each webrequest.

public ResourceProvider(string cultureName)
    {
        this.cultureName = cultureName;
    }

While registering ResourceProvider

container.RegisterType<IResourceProvider, ResourceProvider>(new PerRequestLifetimeManager());
user2873833
  • 263
  • 1
  • 2
  • 6
  • Where do you store the logic to provide a new cultername for each request? – Marcus Höglund May 28 '17 at 09:43
  • Currently a MessageHandler reads it from the HttpContext.Current.Request.UserLanguages and sets it to Thread.CurrentThread.CurrentUICulture. However it is not just about culturename, I need to inject some other similar properties like the correlationId etc. So I am looking for an exiting pattern to do the same. – user2873833 May 28 '17 at 10:26
  • You can use **InjectionFactory**, **InjectionConstructor** (or others **InjectionMember**) during register type, see https://stackoverflow.com/a/16185690/5359302. Also, you can use resolve type with parameters that will be passed into constructor https://stackoverflow.com/a/1457486/5359302 – George Alexandria Jun 01 '17 at 20:19

0 Answers0