我正在构建一个 ASP.NET Core 2.1 应用程序。对于应用程序洞察遥测,我有自定义类,但我也想使用内置的 ITelemetryInitializer。当启用自动交叉连接时,简单注入(inject)器是否会自动解决这些依赖关系?
更新
我尝试了下面的代码并得到了如下所示的错误。我不确定自动交叉接线应该如何工作。
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.'
谢谢
最佳答案
此问题是由 a bug 引起的在简单注入(inject)器的 ASP.NET Core 集成包 4.3.0 版本中。
由于该错误,任何自动跨线依赖关系只能在事件作用域的上下文中解析,即使该依赖关系是 Singleton
。 TelemetryConfiguration
是 Singleton
.
当显式交叉连接该依赖项(即使用 container.CrossWire<TelemetryConfiguration>(app)
)时,问题就会消失,因为 CrossWire
确实允许 Singletons
在事件范围之外解决。
问题已在 patch release 4.3.1 中解决的集成包。在此版本中,您可以解析TelemetryConfiguration
在事件 Web 请求或简单注入(inject)器的上下文之外 Scope
。
但是,如果跨线服务是 Transient
或Scoped
,您仍然需要有一个事件的 Web 请求,或者,如果在后台线程上运行,则需要一个事件的简单注入(inject)器 Scope
.
关于c# - ASP.NET Core 中 UseApplicationInsights/AddApplicationInsights 的简单注入(inject)器用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51201919/