IIS Express强制站点内应用使用首个应用池的问题及解决咨询
我尝试在IIS Express的单个站点中运行两个ASP.NET Core应用,applicationhost.config中的站点配置如下:
<site name="Example" id="1" serverAutoStart="true"> <application path="/" applicationPool="ParentAppPool" preloadEnabled="true"> <virtualDirectory path="/" physicalPath="C:\Path\To\ParentApp" /> </application> <application path="/child" applicationPool="ChildAppPool"> <virtualDirectory path="/" physicalPath="C:\Path\To\ChildApp" /> </application> <bindings> <binding protocol="http" bindingInformation="*:80:dev.example.com" /> <binding protocol="https" bindingInformation="*:443:dev.example.com" /> </bindings> </site>
父应用运行正常,但访问/child时触发500.35错误:
ASP.NET Core does not support multiple apps in the same app pool
Only one in-process application is allowed per IIS application pool. Please assign the application '/LM/W3SVC/1/ROOT/child' to a different IIS application pool.
两个应用已配置为使用不同应用池,且无其他程序占用这两个池。启用traceFailedRequestsLogging后,日志显示访问根路径和/child的请求使用相同processId,且均标记为使用ParentAppPool。
应用池的定义如下:
<add name="ParentAppPool" managedRuntimeVersion="" managedPipelineMode="Integrated" autoStart="true" /> <add name="ChildAppPool" managedRuntimeVersion="" managedPipelineMode="Integrated" autoStart="true" />
进一步测试发现:若父应用使用未定义的应用池,会触发灾难性失败;但若子应用使用未定义的应用池,无报错且仍沿用父应用的池。这说明IIS Express可能强制站点下所有应用使用同一个应用池,导致多应用池配置失效。
方法1:将子应用配置为独立站点
在applicationhost.config中为子应用创建独立站点,使用不同端口或主机名绑定:
<site name="ChildExample" id="2" serverAutoStart="true"> <application path="/" applicationPool="ChildAppPool"> <virtualDirectory path="/" physicalPath="C:\Path\To\ChildApp" /> </application> <bindings> <binding protocol="http" bindingInformation="*:8080:dev.example.com" /> <binding protocol="https" bindingInformation="*:4430:dev.example.com" /> </bindings> </site>
之后可以通过dev.example.com:8080访问子应用,或者修改本地hosts文件添加子域名(如child.dev.example.com)绑定到同一IP,再配置站点绑定对应子域名。
方法2:将其中一个应用改为OutOfProcess模式
修改子应用的项目文件(.csproj),设置托管模式为OutOfProcess:
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
OutOfProcess模式下应用通过Kestrel运行,IIS作为反向代理,此时同一应用池可容纳多个这类应用(但仍建议尽量使用独立池保证隔离性)。
方法3:使用IIS而非IIS Express
IIS完整版支持同一站点下不同应用使用独立应用池的配置,若开发环境允许,可切换到本地IIS部署,直接沿用现有多应用池配置即可生效。
内容的提问来源于stack exchange,提问作者FTWinston

