本文介紹.net6.0中引入的minimal hosting model和如何將.net6.0以前的版本轉(zhuǎn)換成6.0
1. minimal hosting model長啥樣
在入門.net的過程中,我發(fā)現(xiàn)program.cs里面的寫法有些是長這樣的:
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddIniFile("appsettings.ini");
var app = builder.Build();
有些是長這樣的
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
后者看起來更正式一點(diǎn),所以我以為第一種是鬧著玩的,真正的項(xiàng)目是不會這么用的,但是當(dāng)我仔細(xì)看官方文檔的時候發(fā)現(xiàn),其實(shí)是不同版本的aps.net的寫法,第一種是net6.0的,第二種是.net5.0的。
2. 為何引入minimal hosting model
這個結(jié)果真是出乎我意料,所以我就去查了一下為啥要改成鬧著玩的方式(其實(shí)這種方式由自己的名字:minimal hosting model),然后官方的回答是:簡單
對比一下可以發(fā)現(xiàn)確實(shí)簡單了很多,以下是兩個等同的配置,在.net5.0的時候是:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
和
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
}
}
很長,但是有用的就那么幾句,再來看看.net6.0的:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
剛好把有用的幾句摘抄出來了,所以新的版本確實(shí)更簡單了
3.如何轉(zhuǎn)換成minimal hosting model
重要的就兩個:
IServiceCollection
另一個是IApplicationBuilder 和IWebHostEnvironment
轉(zhuǎn)成6.0就是
builder.Services
和app和app.Environment文章來源:http://www.zghlxwxcb.cn/news/detail-402818.html
參考
https://gist.github.com/davidfowl/0e0372c3c1d895c3ce195ba983b1e03d#custom-dependency-injection-container文章來源地址http://www.zghlxwxcb.cn/news/detail-402818.html
到了這里,關(guān)于【.Net |minimal hosting model 】Program.cs 里面的不同寫法的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!