.NET 6-注入服务中的program.cs

发布于 2025-01-21 15:53:59 字数 669 浏览 3 评论 0 原文

我知道如何在.net 5(或之前)中的启动con中进行依赖注入,但是我如何使用.NET 6中的top级program.cs进行同样的操作?

.NET 5:例如,我可以在配置方法中注入一类,

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
    }
}

如何在.NET 6中实现这一目标?

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: for example, I can inject a class in the Configure method

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

神魇的王 2025-01-28 15:53:59

使用.NET 6很容易。只需在配置应用程序服务后执行getService方法,并已运行构建方法。

WebApplication? app = builder.Build();

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

someService.DoSomething();

Using .Net 6 is easy. Just execute GetService method after configure app services and have ran Build method.

WebApplication? app = builder.Build();

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

someService.DoSomething();
云朵有点甜 2025-01-28 15:53:59

如果您需要在开始时使用范围的服务,这就是您的 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();

从:
在应用程序启动上解决服务

它确实适用于mi dbinitializer

If you need to use a scoped service at start, this is how your program.cs should looks like:

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

烟雨扶苏 2025-01-28 15:53:59

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

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

You add your service to the builder.Services collection and then access it with

var myService = services.BuildServiceProvider().GetService<MyService>();
甜中书 2025-01-28 15:53:59

program.cs 文件中,您可以通过 Builder.Services 来管理您

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>();

服务

Inside the program.cs file you can manage your services by builder.Services

For example, I added DbContext and Two different services based on the Singleton pattern and 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

胡渣熟男 2025-01-28 15:53:59

我尝试使用 getService 方法,但我将服务接口类型作为输入(如下所示)传递给我,这与我一起使用。

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

I tried to use the GetService method but I pass the service interface type as input (as shown below) and this worked with me.

var app = builder.Build();    
var injectedService1 = app.Services.GetService<IToInjectService>();
injectedService1.DoSomething();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文