如何在.NET 6中使用CreateHostBuilder(args).Build();

huangapple go评论55阅读模式
英文:

how to use CreateHostBuilder(args).Build(); in .NET 6

问题

我之前在.NET 5中,将这行代码写在Startup.cs中:

var host= CreateHostBuilder(args).Build();
host.MigrateDatabase<Program>();
host.Run();

但是现在我不知道在.NET 6的Program.cs中应该如何使用它。

英文:

I was using .NET 5 and I wrote this line of code in Startup.cs

var host= CreateHostBuilder(args).Build();
host.MigrateDatabase<Program>();
host.Run();

but now I don't know how should I use this in .NET 6 at Program.cs

答案1

得分: 2

.NET 6 引入了 ASP.NET Core 应用程序的新极简托管模型。简而言之,您可以总结更改如下:

var builder = WebApplication.CreateBuilder(args);

// 这里是从 Startup.ConfigureServices 的代码

var app = builder.Build();

// 这里是从 Startup.Configure 的代码

app.Run();

有关更多详细信息,请查看以下内容:

请注意,"旧" 通用主机模型ASP.NET Core 版本)仍然存在,可用(实际上,某些模板如工作程序模板仍在使用它)。

英文:

.NET 6 has introduced the new minimal hosting model for ASP.NET Core apps. In short you can summarize the changes to the following:

var builder = WebApplication.CreateBuilder(args);

// Here goes code from Startup.ConfigureServices

var app = builder.Build();

// Here goes code from Startup.Configure

app.Run();

For more details check the:

Note that the "old" generic host model (ASP.NET Core version) still exists, works and can be used (actually some templates like worker one still use it).

huangapple
  • 本文由 发表于 2023年3月31日 17:50:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897103.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定