英文:
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:
- Migrate from ASP.NET Core 5.0 to 6.0 article
- Code samples migrated to the new minimal hosting model in ASP.NET Core 6.0 article
- ASP.NET Core 6 how to access Configuration during startup
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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论