I am using ApplicationInsights and Hangfire in AspnetCore. Application Insights registers its components as singletons.
My Hangfire job receives a new scope from DI to run in. I have my components registered in DI as scoped lifetime:
services.AddScoped(typeof(IMyJobContext), typeof(MyJobContext))
.
I am creating an object through IServiceProvider.GetService(typeof(IMyJobContext))
in the job’s scope.
Appinsights uses a concept of a TelemetryInitializer
to add some data to the telemetry. I am not able to acquire the same IMyJobContext
object in the TelemetryInitializer
, as it is a singleton and therefore in a different (parent) scope.
I am looking for something similar to aspnet’s IHttpContextAccessor.HttpContext.RequestServices.GetService(typeof(IMyRequestContext))
. Is there such a thing in Hangfire that would allow access to some more specific scope?