gpt4 book ai didi

c# - 什么时候可以使用 `IWebHost.Start()` 方法?

转载 作者:太空狗 更新时间:2023-10-29 18:19:44 25 4
gpt4 key购买 nike

ASP.NET Core 2 MVC。

Microsoft.AspNet.Hosting.IWebHost 接口(interface) contains Start() 方法。此外,Microsoft.AspNetCore.Hosting.WebHostExtensionsdefines IWebHost 接口(interface)的 Run() 扩展方法。

Run() 方法运行 Web 应用程序并阻塞调用线程,直到主机关闭。

同时 Start() 方法不会阻塞调用线程,直到主机关闭。在这种情况下,浏览器会在向用户显示信息之前关闭。

嗯...何时可以使用 IWebHost.Start() 方法?

最佳答案

并非所有托管都是在经典的互联网页面服务场景中执行的。例如,您可能想要从 WPF 应用程序或 Windows 服务提供内容。在这种情况下,您可能不希望调用被阻塞——您的应用程序还有其他事情要做。例如,假设您有一个 WPF 应用程序并且您想要为其中的内容提供服务,您可以简单地扩展 main 方法:

private IWebHost _webHost;

static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

//Create the host
_webHost = WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();

//We want to start, not run because we need the rest of the app to run
_webHost.Start();

//Run the app as normal
Application.Run(new MainForm());

//We're back from the app now, we can stop the host
//...
}

关于c# - 什么时候可以使用 `IWebHost.Start()` 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48186064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com