在生产中实例化ASP.NET Core应用程序 [英] Instantiation of ASP.NET Core application in production

查看:138
本文介绍了在生产中实例化ASP.NET Core应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET Core从Program.cs文件内的Program class中的Main方法开始执行.这将建立一个虚拟主机环境,并告诉该虚拟主机开始运行.然后,有startup.cs文件.

ASP.NET Core starts executing from Main method which lies in Program class inside Program.cs file. That builds a web hosting environment and tell that web host to start running. Then, there is startup.cs file.

在开发.NET Core Web应用程序时,我必须在本地构建它(例如,按Ctrl-F5).这样做时,Main方法正在运行.鉴于IIS Express已经启动,我下次要打开Web应用程序时,我只是在编写例如 http://localhost:65040 .这样,Main方法不再运行,但是一切正常(路由等).因此, 我有以下问题 :

When developing a .NET Core web application, I have to build it locally (by Ctrl-F5 for example). When doing that, Main method is running. Every next time I want to open my web application, given IIS Express has already been launched, I am just writing e.g. http://localhost:65040. By doing that, Main method is not running again, but everything works fine (routing etc..). So, I have the following question:

.NET Core如何在收到上述Http请求后知道如何处理( http://localhost:65040 )?例如,自

How .NET Core knows what to do upon the reception of the above Http request (http://localhost:65040)? For example, how does it implement routing since

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

中的

是否不再运行? There is no need for that because IIS has already been informed?

如果上述想法正确,那么deployment中到底发生了什么?然后,我们的Http请求永远不会触发program.csstartup.cs. 因此,以哪种方式向远程Web服务器通知有关如何实施路由等的信息?

And if the above thought is correct what exactly is happening in deployment? Then our Http requests never trigger program.cs and startup.cs. So, in which way the remote web server is being informed about stuff how to implement routing etc?

推荐答案

经过大量的研究和实验,我发现答案的关键是以下几点: Program.csStartup.cs仅在第一次发出 时运行.这是第一次配置Kestrel并通知它对于任何后续请求(例如路由)必须知道的一切.

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. It is this first time which configures Kestrel and informs it about everything it has to know for any subsequent request (e.g. routing).

老实说,我不知道应用程序如何区分第一个请求和另一个请求,但是我上面描述的确实在开发和生产中都在发生.

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.

这篇关于在生产中实例化ASP.NET Core应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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