IWebHost WebHostBuilder BuildWebHost 有什么区别 [英] What is the difference between IWebHost WebHostBuilder BuildWebHost

查看:34
本文介绍了IWebHost WebHostBuilder BuildWebHost 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读微软文档后,我仍然感到困惑.我需要将我开发的 .net core 2 Web 应用程序部署到 IIS 服务器,但我无法得到任何直接的答案.这只是我问题的开始.

After reading Microsoft documents I am still confused. I need to deploy a .net core 2 web application I developed to an IIS server and I can't get a straight forward answer on anything. This is just the beginning of my questions.

IWebHost、WebHostBuilder、BuildWebHost 有什么区别?

What is the difference between IWebHost, WebHostBuilder, BuildWebHost?

谢谢!

推荐答案

首先,我非常不同意你的说法:ASP.NET Core 的文档其实非常好.是的,它可能仍然缺乏一些细节,并且在赶上发布的变化方面也存在一些问题,但总体而言,内容非常好,并且致力于它的团队确实做得非常出色.为这样一个庞大而快速发展的框架编写文档真的很困难,你通过文档获得的信息量实际上非常好.一旦您在开始使用新框架时克服了最初的问题,您很可能会意识到这一点.

First of all, let me start with that I very much disagree with your statement: The documentation on ASP.NET Core is actually very good. Yes, it may be still lacking in some details, and it also has some problems catching up to changes with releases, but overall the content is really good and the team working on it is really doing a remarkable job. It’s really difficult to author a documentation for such a large and fast-moving framework, and the amount of information you get through the documentation is actually very good. You will likely get to recognize that once you have overcome the initial problems in starting with a new framework.

但回到你的问题:

  • IWebHost:Web 主机是托管和运行您的 Web 应用程序的通用对象.它在您的应用程序启动时创建,然后它将构建所有必要的部分,例如 Kestrel Web 服务器、应用程序中间件管道和所有其他部分,并将它们连接起来,以便您的应用程序准备好为您的请求提供服务.

  • IWebHost: The web host is the general thing that hosts and runs your web application. It gets created when your application starts up, and then it will construct all the necessary pieces, like the Kestrel web server, the application middleware pipeline, and all the other bits, and connects them, so that your application is ready to serve your requests.

网络主机基本上是构成您的网络应用程序的东西.

The web host is basically the thing that makes up your web application.

IWebHostBuilder:Web 主机构建器基本上是一个创建 Web 主机的工厂.它不仅构建了 Web 主机,还配置了 Web 主机确定如何运行 Web 应用程序所需的所有必要部分.

IWebHostBuilder: The web host builder is basically a factory to create a web host. It is the thing that constructs the web host but also configures all the necessay bits the web host needs to determine how to run the web application.

使用 ASP.NET Core 2,您通常会创建一个默认 Web 主机构建器",它已经带有很多默认值.例如,默认 Web 主机将设置 Kestrel Web 服务器、启用和配置日志记录,并添加对 appsettings.json 配置的支持.

With ASP.NET Core 2, you will usually create a "default web host builder" which will already come with a lot defaults. For example, the default web host will set up the Kestrel web server, enable and configure logging, and add support for the appsettings.json configuration.

通常,您的应用程序将始终以这样的默认 Web 主机启动,然后您只需使用 Web 主机构建器在实际构建之前配置 Web 主机.

Usually, your applications will always start with such a default web host and you then just use the web host builder to subsequently configure the web host before it is actually being built.

BuildWebHost 是 ASP.NET Core 2.1 之前的旧约定的一部分,其中 Program.cs 中的默认模式是在分开的方法.在 2.1 中,这一点发生了变化,因此该方法将不再构建网络主机直接而是创建网络主机构建器(因此该方法现在被称为 CreateWebHostBuilder).因此,基本上,Web 主机构建器上的 .Build() 调用已从该方法中重构出来.您可以在 2.0 到 2.1 的迁移指南.

BuildWebHost is part of the older convention before ASP.NET Core 2.1 where the default pattern in the Program.cs was to build the web host in a separate method. With 2.1, this was changed so that the method would no longer build the web host directly but just create the web host builder (hence the method now being called CreateWebHostBuilder). So basically, the .Build() call on the web host builder was refactored out of the method. You can see this nicely in the migration guide for 2.0 to 2.1.

这样做的原因是为了使 CreateWebHostBuilder 可重用.在该方法中发生的构建器配置基本上是配置 Web 主机所需的一切.因此,通过使其可重用,而不生成实际创建的 Web 主机,它可以用于其他目的.在这种情况下,这是使用 TestHost 完成的集成测试.测试主机基本上会在内部为您的集成测试托管 Web 主机,它将通过查找 CreateWebHostBuilder 方法来实现.

The reason this was done is to make the CreateWebHostBuilder reuseable. The builder configuration that happens in that method is basically everything that is necessary to configure the web host. So by making that reusable, without generating an actually created web host, it can be used for other purposes. In this case, this was done for integration testing using the TestHost. The test host will basically host the web host internally for your integration tests, and it will do so by looking for the CreateWebHostBuilder method.

在 ASP.NET Core 2.1 中,您在 Program.cs 中看到的默认模式如下(我添加了注释以进一步解释):

With ASP.NET Core 2.1, the default pattern you see in the Program.cs is the following (comments added by me for further explanations):

public class Program
{
    // main entry point for your application
    public static void Main(string[] args)
    {
        // create the web host builder
        CreateWebHostBuilder(args)
            // build the web host
            .Build()
            // and run the web host, i.e. your web application
            .Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        // create a default web host builder, with the default settings and configuration
        WebHost.CreateDefaultBuilder(args)
            // configure it to use your `Startup` class
            .UseStartup<Startup>();
}

顺便说一句.这个主题通常在应用程序启动托管 官方文档部分.

Btw. this topic is generally covered in the application startup and hosting sections of the official documentation.

从 ASP.NET Core 3.0 开始,我上面描述的设置发生了变化.这样做的原因是 generic主持人.通用主机"是 Web 主机Web 主机构建器 的泛化,允许 ASP.NET Core 之外的非 Web 场景,使 ASP.NET Core本身只是一个运行在通用主机之上的托管服务".

Starting with ASP.NET Core 3.0, there has been a change with the setup I described above. The reason for this is the generic host. The "generic host" is a generalization of the web host and the web host builder, to allow non-web scenarios outside of ASP.NET Core, making ASP.NET Core itself just a "hosted service" that runs on top of the generic host.

  • IHost:主机是承载和运行您的应用程序及其服务的组件.这是之前 IWebHost 的概括,但完成了基本相同的任务:它启动配置的托管服务并确保您的应用程序正在运行和工作.

  • IHost: The host is the component that hosts and runs your application and its services. This is a generalization of the previous IWebHost but fullfills basically the same task: It starts configured hosted services and makes sure that your app is running and working.

IHostBuilder:主机构建器构建主机并配置各种服务.这是对之前的 IWebHostBuilder 的概括,但也基本上对通用的 IHost 做了同样的事情.它在应用程序启动之前配置主机.

IHostBuilder: The host builder constructs the host and configures various services. This is the generalization of the previous IWebHostBuilder but also basically does the same just for generic IHost. It configures the host before the application starts.

Host.CreateDefaultBuilder 方法,它将设置具有各种默认值的主机,例如使用 appsettings.json 和日志进行配置.

There is the Host.CreateDefaultBuilder method that will set up a host with various defaults, e.g. configuration using appsettings.json and logging.

IHostedService:托管服务是 host 托管的核心组件.最常见的示例是 ASP.NET Core 服务器,它作为通用主机之上的托管服务实现.

IHostedService: A hosted service is a central component that the host hosts. The most common example would be the ASP.NET Core server, which is implemented as a hosted service on top of the generic host.

您还可以编写自己的托管服务,或添加第三方服务,以便您可以很好地向应用程序添加内容.

You can also write your own hosted services, or add third-party services that allow you to nicely add things to your application.

ASP.NET Core 3.0 和 .NET Core 3.0 引入的通用主机基本上取代了之前的 IWebHostIWebHostBuilder.它遵循完全相同的架构和想法,但只是简化为非 Web 任务,因此它可以用于许多不同的目的.然后 ASP.NET Core 就建立在这个通用主机之上.

The generic host introduced with ASP.NET Core 3.0 and .NET Core 3.0 basically replaces the previous IWebHost and IWebHostBuilder. It follows the very same architecture and idea but is simply reduced to non-web tasks so that it can work with a number of different purposes. ASP.NET Core then just builds on top of this generic host.

这篇关于IWebHost WebHostBuilder BuildWebHost 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆