I have the following registration code:
private static IContainer BuildContainer()
{
var builder = new ContainerBuilder();
// current assembly
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
...
builder.Register(c => new UserSettings(_connectionString)).As<IUserSettings>().**InstancePerLifetimeScope**();
...
var container = builder.Build();
**CommonServiceLocator.ServiceLocator.SetLocatorProvider(() => new AutofacServiceLocator(container));**
return container;
}
I use also a Microsoft's CommonServiceLocator for one edge case. Inside "OnActionExecuting" function I set up one of the properties in IUserSettings:
public override void OnActionExecuting(HttpActionContext actionContext)
{
...
var userSettings = actionContext.Request.GetDependencyScope().GetService(typeof(IUserSettings)) as IUserSettings;
**userSettings.Test = 123;**
}
In the whole MVC app and inside some libraries, which are MVC app dependencies I can resolve the same object of IUserSettings - if I use a classic DI via constructor.But when I try to resolve it via global service locator:
var settings = ServiceLocator.Current.GetInstance<IUserSettings>();
I get a new instance of IUserSettings, not the initial one which come with the request. Singleton instances works fine, but not those registered as "InstancePerLifetimeScope".
Does anybody know what I did wrong? Is there a way to resolve the same instance of IUserSettings using global "ServiceLocator"?