简体   繁体   English

IHostEnvironment 和启动构造函数中没有 DI .NET Core 3+

[英]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 ConfigureServicesservices.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.

相关问题 .NET Core DI,将参数传递给构造函数的方法 - .NET Core DI, ways of passing parameters to constructor .NET Core DI 没有构造函数参数 - .NET Core DI without constructor arguments ASP.NET 核心3.1:添加框架服务:IHostEnvironment - ASP.NET Core 3.1: Add framework service: IHostEnvironment .Net Core DI - 通过构造函数注入与使用 scope 解析 - .Net Core DI - inject via constructor vs resolve using scope .NET Core 2和DI - 在构造函数中使用appsettings.json中的值? - .NET Core 2 & DI - use values from appsettings.json in the constructor? 在 .NET Core 中使用不同的构造函数参数(DI 和非 DI)注册多个 IHostedService 实例 - Register multiple instances of IHostedService with different constructor parameters (DI and non-DI) in .NET Core MassTransit和.NET Core DI - 如何使用无参数构造函数解决依赖关系? - MassTransit and .NET Core DI - how to resolve dependencies with parameterless constructor? 带有Autofac DI的.NET Core - .NET Core with Autofac DI .NET 核心中的 DI 问题 - DI issue in .NET Core 将 ApplicationInsightsTelemetryProcessor 添加到 DI 时启动时出现 StackOverflowException (ASP.NET Core 2.2) - StackOverflowException at startup when adding ApplicationInsightsTelemetryProcessor to DI (ASP.NET Core 2.2)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM