Reputation: 265
I am building an ASP.NET Core 2.1 app. For app insight telemetry I have my custom class but I also want to use built-in ITelemetryInitializer
's. Does Simple Injector automatically resolves these dependencies when Auto Cross wiring is enabled?
UPDATE
I tried below piece of code and got the error as shown below. I am not sure how else Auto Crosswiring is supposed to work.
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IControllerActivator>(
new SimpleInjectorControllerActivator(container));
services.EnableSimpleInjectorCrossWiring(container);
services.UseSimpleInjectorAspNetRequestScoping(container);
container.AutoCrossWireAspNetComponents(app);
services.AddApplicationInsightsTelemetry(
applicationInsightsLoggerConfig.InstrumentationKey);
var test = Container.GetInstance<TelemetryConfiguration>();
The registered delegate for type TelemetryConfiguration threw an exception. The registered delegate for type IServiceScope threw an exception. The IServiceScope is registered as 'Async Scoped' lifestyle, but the instance is requested outside the context of an active (Async Scoped) scope.'
Thanks
Upvotes: 2
Views: 932
Reputation: 172676
This issue is caused by a bug in version 4.3.0 of the ASP.NET Core integration package for Simple Injector.
Due to the bug, any auto cross-wired dependency can only be resolved within the context of an active Scope, even if the dependency is a Singleton
. TelemetryConfiguration
is a Singleton
.
When explicitly cross-wiring that dependency (i.e., using container.CrossWire<TelemetryConfiguration>(app)
) the problem would go away, since CrossWire
does allow Singletons
to be resolved outside an active scope.
The problem has been resolved in patch release 4.3.1 of the integration package. In this version, you can resolve TelemetryConfiguration
outside the context of an active web request or Simple Injector Scope
.
In case the cross-wired service, however, is Transient
or Scoped
, you still need to either have an active web request, or, in case running on a background thread, an active Simple Injector Scope
.
Upvotes: 2