When do you need SimpleInjector hybrid registration? (How can an IIS hosted object be called without the HttpContext?)
Following on from my recent question regarding SimpleInjector and hybrid web request / lifestyle in thread , it seems I do not fully understand the technical requirements and do what I do, t really need to be done.
With this code
interface IUnitOfWork { }
interface IWebUnitOfWork : IUnitOfWork { }
interface IThreadUnitOfWork : IUnitOfWork { }
class UnitOfWork : IWebUnitOfWork, IThreadUnitOfWork { }
container.RegisterPerWebRequest<IWebUnitOfWork, UnitOfWork>();
container.RegisterLifetimeScope<IThreadUnitOfWork, UnitOfWork>();
container.Register<IUnitOfWork>(() => container.GetInstance<UnitOfWork>());
// Register as hybrid PerWebRequest / PerLifetimeScope.
container.Register<UnitOfWork>(() =>
{
if (HttpContext.Current != null)
return container.GetInstance<IWebUnitOfWork>() as UnitOfWork;
else
return container.GetInstance<IThreadUnitOfWork>() as UnitOfWork;
});
I figured out that for AppDomains running in IIS, there would IWebUnitOfWork
be an error returned , and otherwise there would be an error unless I explicitly declared the LifetimeScope instance to carry the call to the container (which would return IThreadUnitOfWork
).
The following expression made me realize that I didn't quite understand what I was doing!
However, you don't seem to need a hybrid lifestyle. A hybrid lifestyle is a lifestyle that can switch dynamically (on every GetInstance call and every injection) while you feel like you need to switch during startup.
My question is this: under what circumstances does a container (or any other class for that matter), whether static or an instance loaded inside an AppDomain running in IIS, get called without the existence of the HttpContext?
source to share