gpt4 book ai didi

c# - ASP.NET Core 中使用 UseApplicationInsights/AddApplicationInsights 的简单注入(inject)器用法

转载 作者:行者123 更新时间:2023-11-30 23:01:09 25 4
gpt4 key购买 nike

我正在构建一个 ASP.NET Core 2.1 应用程序。对于应用洞察遥测,我有自己的自定义类,但我也想使用内置的 ITelemetryInitializer。启用自动交叉连接后,Simple Injector 会自动解决这些依赖关系吗?

更新

我尝试了下面的一段代码并得到了如下所示的错误。我不确定 Auto Crosswiring 还应该如何工作。

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 引起的在 4.3.0 版的 ASP.NET Core Simple Injector 集成包中。

由于该错误,任何自动 交叉连接的依赖项都只能在事件作用域的上下文中得到解析,即使依赖项是Singleton。 . TelemetryConfigurationSingleton .

当显式交叉连接该依赖项(即使用 container.CrossWire<TelemetryConfiguration>(app) )时,问题就会消失,因为 CrossWire允许 Singletons在事件范围之外解决。

问题已在patch release 4.3.1中解决的集成包。在此版本中,您可以解决 TelemetryConfiguration在事件网络请求或简单注入(inject)器的上下文之外 Scope .

如果交叉线服务是 TransientScoped ,您仍然需要有一个事件的 Web 请求,或者,如果在后台线程上运行,则需要一个事件的简单注入(inject)器 Scope .

关于c# - ASP.NET Core 中使用 UseApplicationInsights/AddApplicationInsights 的简单注入(inject)器用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51201919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com