这一章,我们了解一下launchsettings.json的作用。
打开launchsettings.json 文件后,默认情况下,您将找到以下代码。
{ "iissettings": { "windowsauthentication": false, "anonymousauthentication": true, "iisexpress": { "applicationurl": "http://localhost:1118", "sslport": 0 } }, "profiles": { "iis express": { "commandname": "iisexpress", "launchbrowser": true, "environmentvariables": { "aspnetcore_environment": "development" } }, "webapplication4": { "commandname": "project", "dotnetrunmessages": "true", "launchbrowser": true, "applicationurl": "http://localhost:5000", "environmentvariables": { "aspnetcore_environment": "development" } } } }
在这里,您可以看到,我们有两个部分。 一种用于iis express(iis服务器),另一种用于kestrel服务器。
在visual studio中,您可以找到上述两个配置文件(iis express和webapplication4),如下所示。
如果选择 iis express,则它将使用 iis 服务器,如果您选择 webapplication4 应用程序,则它将使用 kestrel 服务器。
launchsettings.json文件的 commandname 属性的值可以是以下任何一个:
- iisexpress
- iis
- project
launchsettings.json文件的commandname属性值以及应用程序项目文件中的aspnetcorehostingmodel元素值将确定将要使用和处理传入http请求的内部和外部web服务器(反向代理服务器)。 为了更好地理解,请看下图。
如何验证其工作模式?
方法1:
当我们在launchsettings.json文件中将commandname用作project时,asp.net core将忽略aspnetcorehostingmodel值。 kestrel是唯一将托管应用程序并处理传入请求的服务器。 让我们证明这一点。 现在,我们需要将启动profile设置为webapplication4,如下所示。
如果查看launchsettings.json文件,那么您将看到webapplication4配置文件使用"commandname":"project"值,并且请注意下面所示的应用程序url。
在我的应用程序中,url为http://localhost:5000,端口号在您的示例中可能有所不同。
"webapplication4": { "commandname": "project", "dotnetrunmessages": "true", "launchbrowser": true, "applicationurl": "http://localhost:5000", "environmentvariables": { "aspnetcore_environment": "development" } }
现在,将应用程序项目文件中的aspnetcorehostingmodel元素值更改为inprocess,如下所示。
<project sdk="microsoft.net.sdk.web"> <propertygroup> <targetframework>net5.0</targetframework> <aspnetcorehostingmodel>inprocess</aspnetcorehostingmodel> </propertygroup> </project>
然后, 启动该项目, 可以看到首先启动了cmd,在这种情况下将使用kestrel服务器托管应用程序。
注: 因为当commandname值为project时,它将忽略aspnetcorehostingmodel值,而kestrel是唯一将托管和处理传入请求的服务器。
方法2:
如果我们使用命令名作为 iisexpress 配置文件,如果我们将 aspnetcorehostingmodel 值设置为 outofprocess,则asp.net core使用iis express作为外部 web 服务器,kestrel 是内部 web 服务器。外部 web 服务器(即 iis express)将接收传入的 http 请求,然后将请求转发到内部 web 服务器,即将处理请求的 kestrel。让我们证明这一点。
由于我们已经将启动配置文件设置为 iis express,我们只需要将 aspnetcorehostingmodel 元素值更改为应用程序的项目文件中的 outofprocess,如下所示。
<project sdk="microsoft.net.sdk.web"> <propertygroup> <targetframework>net5.0</targetframework> <aspnetcorehostingmodel>outofprocess</aspnetcorehostingmodel> </propertygroup> </project>
运行应用程序,浏览器输出了项目名称, 如下所示, 因为请求由kestrel web server服务器处理。
注: 通过运行时,输出当前进程名称即可。
到此这篇关于asp.net core基础之启动设置的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持代码网。
发表评论