I am using Hangfire and Autofac in a aspnetcore project. So far so good. I have some CorrelationContext
class that stores a guid (correlationid). I have created a ICorrelationContextAccessor
interface that has a property used to access CorrelationContext
.
I have two ICorrelationContextAccessor implementations:
-
HttpContextCorrelationContextAccessor
gets theCorrelationContext
from the http requestcontext (if in a webapi request) -
JobContextCorrelationContextAccessor
one gets it from a job’s context which is an instanceperlifetime object
My idea was that I could use two containers, one for jobs and one for http requests and register these two accessors as singleinstance in one of them.
var builder = new Autofac.ContainerBuilder();
builder.RegisterType<HttpContextCorrelationContextAccessor>().As<ICorrelationContextAccessor>().SingleInstance();
ApplicationContainer = builder.Build();
var builderForJob = new Autofac.ContainerBuilder();
builderForJob.RegisterType<JobContextCorrelationContextAccessor>().As<ICorrelationContextAccessor>().SingleInstance();
var JobContainer = builderForJob.Build();
GlobalConfiguration.Configuration.UseAutofacActivator(JobContainer);
When running jobs though, I see that my ICorrelationContextAccessor
is of type HttpContextCorrelationAccessor
. Why is this? How would I use a different approach to get my CorrelationContext
in a job and a httprequest?