C# Asp.net Core 中的 UseIISIntegration 有什么用?

csharpserver side programmingprogramming更新于 2025/9/30 12:52:17

所有 ASP.NET Core 应用程序都需要一个 WebHost 对象,该对象实际上充当应用程序和 Web 服务器。WebHostBuilder 用于配置和创建 WebHost。您通常会在 WebHostBuilder 设置代码中看到 UseKestrel() 和 UseIISIntegration()。

它们有什么作用?

UseKestrel() − 这会将 Kestrel 的 IServer 接口注册为用于托管应用程序的服务器。

未来可能会有其他选项,包括仅适用于 Windows 的 WebListener。

UseIISIntegration() −这告诉 ASP.NET,IIS 将在 Kestrel 之前作为反向代理运行。

然后,这将指定一些设置,包括 Kestrel 应侦听的端口、转发标头以及其他详细信息。

示例

public class Program{
   public static void Main(string[] args){
      var host = new WebHostBuilder()
      .UseKestrel()
      .UseContentRoot(Directory.GetCurrentDirectory())
      .UseIISIntegration()
      .UseStartup()
      .Build();
      host.Run();
   }
}

在 ASP.NET Core 2.2 之前,ASP.NET Core 是在 IIS 进程外托管的,因此一个应用程序有两个进程,减去

w3wp.exe IIS 进程

dotnet.exe ASP.NET Core 进程,Kestrel Web 服务器在此启动。这意味着 IIS 和 Kestrel 在这两个进程之间进行通信。

对于此场景,我们使用 UseIISIntegration。


相关文章