【发布时间】:2020-02-07 14:46:00
【问题描述】:
我有一个使用 Application Insights 进行遥测的 ASP.NET Core Web 应用程序。我想用我自己的接口包装 TelemetryClient 对象并将该实例添加到服务集合中。为此,我需要访问 startup.cs 中 Telemetry 客户端的单例实例。我可以使用此代码获取服务信息:
var info = services.Where(x => x.ServiceType.Name == "TelemetryClient").Single();
该信息有一个我希望在其中找到实例的 ImplementationInstance,但它为空。有没有办法获得这个项目的单例实例,以便我可以用它创建我的包装类实例?我的另一个选择是在我的控制器构造函数中创建包装类,因为我可以通过以下代码从服务容器访问它:
HttpContext.RequestServices.GetService(typeof(T)) as T;
任何帮助将不胜感激。谢谢。
【问题讨论】:
标签: asp.net-core dependency-injection azure-application-insights