![](/img/trans.png)
[英]What is the difference between Host and WebHost class in asp.net core
[英]What's the difference between these ways to start/run a Generic Host in ASP.NET Core?
ASP.NET Core 中的托管設計現在有一個新的通用主機(.NET Core 2.1+),它將在未來取代 Web 主機。
有很多方法可以使用Microsoft.Extensions.Hosting
接口IHost
和IHostBuilder
來啟動應用程序。
我知道使用async
與sync
之間的區別,但是所有這些選項之間有什么區別? 使用Run
vs Start
並調用IHostBuilder
與調用IHost
?
請參閱以下代碼中的選項// 1
、 // 2
、 // 3
和// 4
:
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyNamespace
{
class Program
{
static async Task Main(string[] args)
{
IHostBuilder builder = CreateBuilder();
// 1 - Call Run on the builder (async)
await builder.RunConsoleAsync(); // extension method
// 2 - Call Start on the builder (sync)
builder.Start(); // extension method
IHost host = builder.Build(); // Call Build on the builder to get a host
// 3 - Call Run on the host (sync / async)
host.Run(); // extension method
await host.RunAsync(); // extension method
// 4 - Call Start on the host (sync / async)
host.Start(); // extension method
await host.StartAsync(); // class method
}
private static IHostBuilder CreateBuilder() => new HostBuilder()
.ConfigureAppConfiguration((hostingContext, config) =>
{
//...
})
.ConfigureLogging((hostingContext, logging) => {
//...
})
.ConfigureServices((hostContext, services) =>
{
//...
services.AddSingleton<IHostedService, MyService>();
});
}
}
為 .NET 6 更新。
RunConsoleAsync
之外,所有這些方法都在IHost
實例上運行。 IHostBuilder
上的那些只是調用IHost.Build()
然后委托給IHost
方法(例如IHostBuilder.StartAsync()
等效於IHostBuilder.Build().StartAsync()
)。.GetAwaiter().GetResult()
)的包裝。Task IHost.StartAsync(CancellationToken cancellationToken = default)
啟動主機(Web 應用程序)。 主機啟動后任務完成。
void Start(this IHost host)
IHost.StartAync
的同步包裝器。
Task RunAsync(this IHost host, CancellationToken token = default)
{
using (host)
{
await host.StartAsync(token);
await host.WaitForShutdownAsync(token);
}
}
啟動主機。 任務在主機關閉時完成,可以通過取消令牌或在另一個線程上調用StopAsync()
來觸發。
Task WaitForShutdownAsync(this IHost host, CancellationToken token = default)
返回應用程序關閉時完成的任務。 通過傳遞的令牌啟動關閉,取消令牌會導致應用程序停止。
void WaitForShutdown(this IHost host)
IHost.WaitForShutdownAync
的同步包裝器。
Task IHost.StopAsync(CancellationToken cancellationToken = default)
優雅地停止主機,返回一個在主機停止后完成的任務。 取消cancellationToken
表示停止不應再是優雅的。
還有一個擴展方法允許傳遞Timeout
:
public static Task StopAsync(this IHost host, TimeSpan timeout)
=> host.StopAsync(new CancellationTokenSource(timeout).Token);
Task RunConsoleAsync(this IHostBuilder hostBuilder, CancellationToken cancellationToken = default)
{
return hostBuilder.UseConsoleLifetime().Build().RunAsync(cancellationToken);
}
這是一個奇怪的人,因為:
IHost
的相應實現Start
方法查看該方法的實際代碼可以清楚地了解前者奇怪的原因,但我不確定后者。
// 1 - 在構建器上調用 Run(異步)
RunConsoleAsync 啟用控制台支持,構建和啟動主機,並等待 Ctrl+C/SIGINT 或 SIGTERM 關閉。 因此,正如其名稱所預期的那樣,它僅用於在控制台中托管您的應用程序(而不是 IIS 等)
// 2 - 在構建器上調用 Start(同步)
只是同步啟動主機
public static IHost Start(this IHostBuilder hostBuilder)
{
var host = hostBuilder.Build();
host.StartAsync(CancellationToken.None).GetAwaiter().GetResult();
return host;
}
// 3 - 在主機上調用 Run(同步/異步)
RunAsync 運行應用程序並返回一個在觸發取消令牌或關閉時完成的任務。 Sync 只是一個包裝器:
public static void Run(this IHost host)
{
host.RunAsync().GetAwaiter().GetResult();
}
// 4 - 在主機上調用開始(同步/異步)
這個方法實際上是在啟動程序,並且最終會從任何其他方式調用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.