[英]IHostEnvironment and no DI in Startup constructor .NET Core 3+
We are building .NET Core microservices.我们正在构建 .NET Core 微服务。
Many of our microservices use HttpClient
logging through ConfigurePrimaryMessageHandler()
我们的许多微服务通过ConfigurePrimaryMessageHandler()
使用HttpClient
日志记录
With .NET Core 2+ our approach had been to使用 .NET Core 2+ 我们的方法是
services.AddHttpClient().ConfigurePrimarymessageHandler()
in ConfigureServices
of Startup.cs
. Startup.cs
ConfigureServices
的services.AddHttpClient().ConfigurePrimarymessageHandler()
。 We had to pass in a ILogger<T>
here and we would get that by injecting it into our Startup constructor.我们必须在这里传入一个ILogger<T>
,我们可以通过将它注入我们的 Startup 构造函数来获得它。
Now that we have migrated to IHostEnvironment
we can no longer use DI to inject ILogger<T>
into Startup constructor AND we can't inject IServiceCollection
into Configure()
.现在我们已经迁移到IHostEnvironment
我们不能再使用 DI 将ILogger<T>
注入到启动构造函数中,也不能将IServiceCollection
注入到Configure()
。
One idea I had was to create the HttpClientBuilder
, store a reference to it and then do the ConfigurePrimaryMessageHandler
in Configure
... This did NOT work.我的一个想法是创建HttpClientBuilder
,存储对它的引用,然后在ConfigurePrimaryMessageHandler
中执行Configure
... 这不起作用。 I'm assuming because once it is built, it is built so you need to do it in ConfigureServices
.我假设因为一旦它被构建,它就会被构建,所以你需要在ConfigureServices
完成它。
So I ended up coming up with a nasty hack that works but it feels yucky:所以我最终想出了一个令人讨厌的黑客,但它感觉很糟糕:
I have an ILogger<T>
as a protected member of my Startup.cs
.我有一个ILogger<T>
作为我的Startup.cs
的受保护成员。
I pass that in (its null) into my HttpClientBuilder
s...我将它传入(它的空值)到我的HttpClientBuilder
s ...
Then in Configure()
I inject ILogger<T>
and set the protected member variable.然后在Configure()
我注入ILogger<T>
并设置受保护的成员变量。
Yuck!糟糕!
It kind of looks like this:它看起来像这样:
class Startup
{
protected ILogger<HttpClientLoggingHandler> _logHack;
public void ConfigureServices(IServiceCollection)
{
services.AddHttpClient<T>().ConfigurePrimaryMessageHandler(()=>{ new
HttpClientLoggingHandler(_logHack, otherstuff) });
}
public void Configure(IApplicationBuilder bld, ILogger<T> logForHack, otherstufffordi)
{
_logHack = logForHack; // note this works, but is this really a good design pattern?
}
}
So this does work... But it doesn't feel like a really great design pattern.所以这确实有效......但它感觉不像是一个非常好的设计模式。 Any advice out there anyone?有人有什么建议吗? Is this really the approach we should be taking?这真的是我们应该采取的方法吗? Feel like something was missed here when we migrated to IHostEnvironment
.当我们迁移到IHostEnvironment
时,感觉这里遗漏了一些东西。
You can simply do .ConfigurePrimaryMessageHandler<HttpClientLoggingHandler>();
你可以简单地做.ConfigurePrimaryMessageHandler<HttpClientLoggingHandler>();
, provided the HttpClientLoggingHandler
is registered. ,前提是HttpClientLoggingHandler
已注册。
The lambda overloads for ConfigurePrimaryMessageHandler
also can pass you an IServiceProvider
. ConfigurePrimaryMessageHandler
的 lambda 重载也可以向您传递IServiceProvider
。 From there, call sp.GetRequiredService<T>
.从那里,调用sp.GetRequiredService<T>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.