IWebHost WebHostBuilder BuildWebHost和有什么不一样 [英] What is the difference between IWebHost WebHostBuilder BuildWebHost

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

问题描述

Microsoft的文档绝对糟糕. 我需要将我开发的.net core 2 Web应用程序部署到IIS服务器,但无法获得任何直接答案.这只是我的问题的开始.

Microsoft's documentation is absolutely terrible. 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.

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

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 Host构建器随后在实际构建Web Host之前对其进行配置.

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中的默认模式是使用单独的方法来构建Web主机.在2.1中,对此进行了更改,以使该方法不再直接通过 build 构建Web主机,而只是通过 create 构建Web主机构建器(因此该方法现在称为CreateWebHostBuilder) .因此,基本上,Web Host Builder上的.Build()调用已从该方法中重构出来.您可以在

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开始,我上面描述的设置发生了变化.原因是通用主机. 通用主机"是 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: 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: 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: 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天全站免登陆