We're using Sitecore 8.2, habitat.
I have a service that I need to use throughout a full HTTP Request. This service should be instantiated once (we use dependancy injection) and then be available everywhere else through DI. I have added the service as AddScoped<T>
and this is working correctly when I'm instantiating the service in controllers for example.
However, the business requirements have now changed, and we need this service available during all times of the execution of a httpRequest
.
Normally speaking I'd just edit the global.asax file (RequestBegin and RequestEnd) to instantiate the service, but in Sitecore 8.2 this is bad-practise.
What have I tried:
I've added a pipeline processor (httpRequestBegin
and httpRequestEnd
) with resolve=true
to resolve the service from the constructor of the processor.
Result: The application is resolving the service like it should, however it doesn't follow the dependancy injection rules of AddScoped<T>
because I see the service being instantiated a second time on controllers later on in the request pipeline. This means the service is instantiated twice, which is a problem for me (we need 1 service per request).
Perhaps someone can point me in the right direction - why does sitecore instantiate the service twice in this case?
Note: it instantiates it once for the processor, and then only once for the all controllers and other services / repositories being called. So it is working, but it seems there are two scopes or something.
Any tips / help in the right direction?