简体   繁体   English

将现有的 IServiceCollection 和 ILoggerFactory 传递给 .NET Core 2 中的 Startup

[英]Pass existing IServiceCollection and ILoggerFactory to Startup in .NET Core 2

I have a console-application that hosts a Web API.我有一个托管 Web API 的控制台应用程序。 Now I want to pass an already configured IServiceCollection and ILoggerFactory to my Startup .现在我想将已经配置的IServiceCollectionILoggerFactory给我的Startup

var serviceCollection = new ServiceCollection();
// Do some registrations here...

var loggerFactory = new LoggerFactory(); // Actually not created this way. Just an example.
loggerFactory.AddSomeStuff();

var host = WebHost.CreateDefaultBuilder()
    .UseKestrel()
    .ConfigureServices(collection =>
    {
        // I want to use my already configured serviceCollection.
        // I do not want to configure it here...
    })
    .ConfigureLogging((hostingContext, logging) =>
    {
        // I want to use my already configured ILoggerFactory.
        // I do not want to configure it here...
    })
    .UseStartup<Startup>()
    .Build();

Basically I want my Startup to use my already created loggerFactory and serviceCollection .基本上我希望我的 Startup 使用我已经创建的loggerFactoryserviceCollection Is that possible?这可能吗? And if so, how do I do it?如果是这样,我该怎么做?

The WebHost's Build method is instantiating an instance of ServiceCollection() class as a method variable and it is passed to every Action delegate (example: ConfigureService(Action<IServiceCollection>configureService)) . WebHost 的 Build 方法将 ServiceCollection() 类的实例实例化为方法变量,并将其传递给每个 Action 委托(例如: ConfigureService(Action<IServiceCollection>configureService)) It seems there is no way of replacing that with custom one except making own implementation of IWebHost (which can introduce all sorts of problems).除了自己实现 IWebHost (这会引入各种问题)之外,似乎没有办法用自定义的方法替换它。 Regards.问候。

Not possible: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-3.1#create-logs-in-the-startup-class不可能: https : //docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-3.1#create-logs-in-the-startup-class

Writing logs before completion of the DI container setup in the Startup.ConfigureServices method is not supported:不支持在 Startup.ConfigureServices 方法中完成 DI 容器设置之前写入日志:

  • Logger injection into the Startup constructor is not supported.不支持将 Logger 注入到 Startup 构造函数中。
  • Logger injection into the Startup.ConfigureServices method signature is not supported不支持将 Logger 注入 Startup.ConfigureServices 方法签名

The reason for this restriction is that logging depends on DI and on configuration, which in turns depends on DI.此限制的原因是日志记录取决于 DI 和配置,而后者又取决于 DI。 The DI container isn't set up until ConfigureServices finishes.在 ConfigureServices 完成之前,不会设置 DI 容器。

You can add a constructor parameter for ILoggerFactory to your Startup class constructor.您可以将 ILoggerFactory 的构造函数参数添加到 Startup 类构造函数。

Then you can use it in the ConfigureServices method.然后您可以在 ConfigureServices 方法中使用它。

public class Startup
{
    readonly ILoggerFactory loggerFactory;

    public Startup(ILoggerFactory loggerFactory)
    {
        this.loggerFactory = loggerFactory;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        // Use it here
        loggerFactory.CreateLogger<..>();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM