简体   繁体   English

.NET 6 - 将服务注入 program.cs

[英].NET 6 - Inject service into program.cs

I know how to do dependency injection in the Startup.cs in .NET 5 (or before), but how do I do the same with the top-level Program.cs in .NET 6?我知道如何在 .NET 5(或之前)的 Startup.cs 中进行依赖注入,但是我如何对 .NET 6 中的顶级 Program.cs 进行同样的操作?

.NET 5: for example, I can inject a class in the Configure method .NET 5:比如我可以在Configure方法中注入一个class

public class Startup
{
    public IConfiguration _configuration { get; }
    public IWebHostEnvironment _env { get; set; }

    public Startup(IConfiguration configuration, IWebHostEnvironment env)
    {
        _configuration = configuration;
        _env = env;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        // TODO
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IToInjectService serviceToInject)
    {
        // USE SERVICE
    }
}

How can I achieve this in .NET 6?我怎样才能在 .NET 6 中实现这一点?

您将服务添加到builder.Services集合中,然后使用

var myService = services.BuildServiceProvider().GetService<MyService>();

Using .Net 6 is easy.使用 .Net 6 很容易。 Just execute GetService method after configure app services and have ran Build method.只需在配置应用服务并运行 Build 方法后执行 GetService 方法。

WebApplication? app = builder.Build();

var someService = app.Services.GetService<SomeService>();

someService.DoSomething();

Inside the program.cs file you can manage your services by builder.Servicesprogram.cs文件中,您可以通过builder.Services管理您的服务

For example, I added DbContext and Two different services based on the Singleton pattern and Scoped例如,我添加了 DbContext 和两个基于 Singleton 模式和 Scoped 的不同服务

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddDbContext<MyDbContext>(options =>
{
    // options.UseSqlServer(...);
});
builder.Services.AddSingleton<IMyService, MyService>();
builder.Services.AddScoped<IMySessionBasedService, MySessionBasedService>();

For more information check Code samples migrated to the new minimal hosting model in ASP.NET Core 6.0有关更多信息,请查看代码示例迁移到 ASP.NET Core 6.0 中的新最小托管模型

I tried to use the GetService method but I pass the service interface type as input (as shown below) and this worked with me.我尝试使用GetService方法,但我将服务接口类型作为输入传递(如下所示),这对我有用。

var app = builder.Build();    
var injectedService1 = app.Services.GetService<IToInjectService>();
injectedService1.DoSomething();

If you need to use a scoped service at start, this is how your program.cs should looks like:如果您需要在开始时使用范围服务,那么您的program.cs应该如下所示:

var builder = WebApplication.CreateBuilder(args);

//Add the service
builder.Services.AddScoped<IMyDependency, MyDependency>();

var app = builder.Build();

using (var serviceScope = app.Services.CreateScope())
{
    var services = serviceScope.ServiceProvider;

    var myDependency = services.GetRequiredService<IMyDependency>();

    //Use the service
    myDependency.DoSomething();

}

app.Run();

from: Resolve a service at app start up来自: 解决应用程序启动时的服务

it did work for mi DbInitializer它确实适用于 mi DbInitializer

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

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