简体   繁体   English

在生产中实例化ASP.NET Core应用程序

[英]Instantiation of ASP.NET Core application in production

ASP.NET Core starts executing from Main method which lies in Program class inside Program.cs file. ASP.NET Core从Main方法开始执行, Main方法位于Program.cs文件中的Program class That builds a web hosting environment and tell that web host to start running. 这构建了一个Web托管环境,并告诉Web主机开始运行。 Then, there is startup.cs file. 然后,有startup.cs文件。

When developing a .NET Core web application, I have to build it locally (by Ctrl-F5 for example). 在开发.NET Core Web应用程序时,我必须在本地构建它(例如,通过Ctrl-F5)。 When doing that, Main method is running. 这样做时, Main方法正在运行。 Every next time I want to open my web application, given IIS Express has already been launched, I am just writing eg http://localhost:65040 . 每次我想打开我的Web应用程序时,鉴于IIS Express已经启动,我只是编写例如http:// localhost:65040 By doing that, Main method is not running again, but everything works fine (routing etc..). 通过这样做, Main方法不再运行 ,但一切正常(路由等..)。 So, I have the following question : 所以, 我有以下问题

How .NET Core knows what to do upon the reception of the above Http request ( http://localhost:65040 )? .NET Core如何知道在接收上述Http请求时该怎么做( http:// localhost:65040 )? For example, how does it implement routing since 例如,它是如何实现路由的

app.UseMvc(routes =>
{
    app.UseMvcWithDefaultRoute();
})

in Startup.cs is not running again? Startup.cs中没有再次运行? There is no need for that because IIS has already been informed?

And if the above thought is correct what exactly is happening in deployment ? 如果上述想法是正确的,那么deployment究竟发生了什么? Then our Http requests never trigger program.cs and startup.cs . 然后我们的Http请求永远不会触发program.csstartup.cs So, in which way the remote web server is being informed about stuff how to implement routing etc? 那么,远程Web服务器以哪种方式被告知如何实现路由等?

Regardless of hosting you choose for ASP.Net Core Application (IIS or self hosting via Kestrel), methods Program.Main() , Startup.ConfigureServices() and Startup.Configure() are executed exactly once during hosting process startup. 无论您选择ASP.Net核心应用程序(IIS还是通过Kestrel进行自托管),在宿主进程启动期间,方法Program.Main()Startup.ConfigureServices()Startup.Configure()执行一次。

It's obvious that Program.Main() is executed when you launch exe file with Kestrel web server within. 很明显,当您使用Kestrel Web服务器启动exe文件时,会执行Program.Main() It could be however not obvious whether it's actually called when hosting in IIS. 然而,在IIS中托管时是否实际调用它可能并不明显。 It is so actually. 实际上是这样的。 When ASP.Net Core Application is integrated with IIS, it's usually executed by dotnet.exe runner (it's also possible to configure the launch of application .exe file). 当ASP.Net核心应用程序与IIS集成时,它通常由dotnet.exe运行程序执行(它也可以配置应用程序.exe文件的启动)。 You could check it in web.config created during adding of application: 您可以在添加应用程序期间创建的web.config中进行检查:

<configuration>
  <system.webServer>
    <!-- ... -->
    <aspNetCore processPath="dotnet" arguments=".\TestMvcApplication.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
  </system.webServer>
</configuration>

So it basically the same as when you run application with command dotnet.exe TestMvcApplication.dll . 所以它与使用命令dotnet.exe TestMvcApplication.dll运行应用程序时基本相同。 Program.Main() is executed in this case, with Startup methods following. 在这种情况下执行Program.Main() ,后面是Startup方法。

Without Main() method that basically builds the web host, ASP.Net Core application just can not run. 如果没有基本构建Web主机的Main()方法,ASP.Net Core应用程序就无法运行。

When you fire subsequent queries, they are processed by the same hosting application (I mean the same Windows process here). 当您触发后续查询时,它们由同一主机应用程序处理(我的意思是这里的Windows进程相同)。 And all configuration required by ASP.Net Core (like routing, middleware, services etc.) is already wired up within this process. ASP.Net Core所需的所有配置(如路由,中间件,服务等)已经在此过程中连接。 That's why ASP.Net Core is able to process the requests. 这就是ASP.Net Core能够处理请求的原因。

I hope this is an answer for your question. 我希望这是你的问题的答案。 Here are some useful links that could be helpful: 以下是一些有用的有用链接:

Hosting in ASP.NET Core 在ASP.NET Core中托管

Host ASP.NET Core on Windows with IIS 使用IIS在Windows上托管ASP.NET Core

Application startup in ASP.NET Core ASP.NET Core中的应用程序启动

Here's what happens when you run your program from a high level view: 以下是从高级视图运行程序时发生的情况:

First like any other .NET application the Main method will be called, since that's the starting point in any .NET application. 首先,与任何其他.NET应用程序一样,将调用Main方法,因为这是任何.NET应用程序的起点。

If it's a console app then nothing special, it just runs the codes and exits the program. 如果它是一个控制台应用程序,那么没什么特别的,它只是运行代码并退出程序。

But for web applications the program needs to run forever and listen to incoming HTTP requests. 但是对于Web应用程序,程序需要永远运行并监听传入的HTTP请求。 So how can we say to our app to do this for us? 那么我们怎么能对我们的应用程序说这个呢? We have to configure it. 我们必须配置它。 Like any other framework, .NET Core has some rules and conventions, here's the simplest way you can build a web app in .NET Core: 与任何其他框架一样,.NET Core有一些规则和约定,这是在.NET Core中构建Web应用程序的最简单方法:

public class Program
{
    public static void Main()
    {
        new WebHostBuilder()
            .UseKestrel()
            .Configure(app =>
            {
                app.Run(async context => await context.Response.WriteAsync("Hello World!"));
            })
            .Build()
            .Run();
    }
}

So what happens here is that the OS runs the Main method in a process (with a processing thread). 那么这里发生的是OS在一个进程中运行Main方法(带有一个处理线程)。 We create a WebHostBuilder and configure it to use Kestrel as webserver and send back "Hello World!" 我们创建一个WebHostBuilder并将其配置为使用Kestrel作为webserver并发送回“Hello World!” string to any incoming HTTP request. 字符串到任何传入的HTTP请求。 Then we build and run our WebHostBuilder . 然后我们构建并运行我们的WebHostBuilder

What Configure method does is that it creates a pipeline so every request will go through the logic of that pipeline. Configure方法的作用是创建一个管道,这样每个请求都将通过该管道的逻辑。 All the method you see in the Configure method will just add extra logic to the pipeline like authentication, static content serving, configuring routes and etc. 您在Configure方法中看到的所有方法都只会向管道添加额外的逻辑,如身份验证,静态内容服务,配置路由等。

I should note here that for using some features we have to add them to our program first, which will be done in ConfigureServices() method, which runs before Configure() method. 我要在这里指出,对使用某些功能,我们必须将它们添加到我们的程序首先,将在完成ConfigureServices()方法,该方法之前运行Configure()方法。

The important method here is Run() , which blocks the calling thread (the thread that called the Main method). 这里重要的方法是Run() ,它阻塞调用线程(调用Main方法的线程)。 So the Main method will never complete, otherwise it would exit the program. 所以Main方法永远不会完成,否则它会退出程序。 But our WebHost is running in the background and will respond to HTTP requests. 但我们的WebHost正在后台运行,并将响应HTTP请求。

So to wrap up, the Main method will run only once during the application startup and it creates and configures a WebHost to serve the HTTP requests. 因此,要进行总结, Main方法将在应用程序启动期间仅运行一次,并创建和配置WebHost以提供HTTP请求。 There is no need to run the Main method or create a new WebHost for every request. 无需为每个请求运行Main方法或创建新的WebHost

These were some basics, if you want to learn more you should read Microsoft Docs. 这些是一些基础知识,如果你想了解更多,你应该阅读Microsoft Docs。 There are many useful informations there. 那里有许多有用的信息。

After a lot of studying and experiments, I found that the key point into my answer is the following: Program.cs and Startup.cs are running only the very first time an Http request is being made. 经过大量的学习和实验,我发现我的答案的关键点如下: Program.csStartup.cs 仅在第一次发出Http请求运行。 It is this first time which configures Kestrel and informs it about everything it has to know for any subsequent request (eg routing). 这是第一次配置Kestrel并告知它任何后续请求(例如路由)必须知道的一切。

To be honest, I do not know how exactly the application distinguishes between the very first request and the other ones , however what I described above is definitely happening to both development and production. 说实话,我不知道应用程序如何区分第一个请求和其他请求 ,但我上面描述的内容肯定发生在开发和生产中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在“生产”环境中Web部署ASP.NET Core应用程序? - How to WebDeploy an ASP.NET Core application in “Production” environment? ASP.NET 相当于生产环境的 Core 6 launchUrl - ASP.NET Core 6 launchUrl equivalent for Production 将ASP.Net核心Web应用程序编写的微服务部署到生产环境的正确方法是什么 - what is the proper way to deploy microservices written with ASP.Net core web application to production Asp.net Core 2.1 NullReferenceException 生产中但不是开发中的错误 - Asp.net Core 2.1 NullReferenceException Error in Production but Not Development ASP.NET 内核:生产连接串的放置位置 - ASP.NET Core: Where to place Connection String for Production 将ASP.Net Core集成到WPF应用程序中 - Integrate ASP.Net Core into an WPF application ASP.NET Core 2.0中的应用程序变量 - Application Variables in ASP.NET Core 2.0 具有Application Insights的asp.net core 2记录器 - asp.net core 2 Logger with Application Insights 在分层应用程序ASP.NET Core中进行验证 - Validation in a layered application ASP.NET Core ASP.NET Core日志记录不适用于生产环境 - ASP.NET Core Logging does not work on Production
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM