在同一主机和端口下运行多个应用 [英] Running multiple apps under same host and port

查看:140
本文介绍了在同一主机和端口下运行多个应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个解决方案中开发多个asp.net核心应用程序,当我使用IIS Express在Visual Studio中运行该解决方案时,希望它们具有相同的URL和端口.例如:

I am developing multiple asp.net core applications within one solution and when I run the solution in Visual Studio using IIS Express, would like to have them under the same url and port. For example:

  • App1: https://localhost:44369
  • App2: https://localhost:44369/App2
  • App3: https://localhost:44369/App3

我尝试将lauchSettings.json文件更改为使用相同的主机和url,但是出现错误,表明服务器无法运行.

I've tried to change the lauchSettings.json file to use the same host and url but then an error comes up that the server can't run.

是否可以配置它?

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:44369/App2",
      "sslPort": 44369
    }
  },
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "App2": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
    }
  }
}

推荐答案

我正在逐渐将一个大型ASPX应用程序从.Net Framework 4.8迁移到.Net5.今天,我遇到了类似的问题.通过IIS Express一起运行两个.Net Framework应用程序可以正常运行",但对于.Net Core/5来说,运行起来不一定那么容易.我希望将网站的旧版部分托管在现有的Web项目中,并作为根目录的一部分.网站,我希望将网站的.Net 5部分托管在根目录下的虚拟目录中.我的两个站点需要一起工作,因为两个应用程序之间共享了基于声明的身份验证,但是我很难过一会儿就使Visual Studio IIS Express集成在两个项目之间正常且一致地工作.对我来说,使它起作用的方法如下:

I'm gradually moving a large ASPX app from .Net Framework 4.8 to .Net 5. I ran into a similar problem today. Running two .Net Framework apps together via IIS Express 'just works', but it doesn't necessarily work as easily for .Net Core / 5. I wished to host the legacy portion of the site in the existing web project and root of the website, and I wish to host the .Net 5 portion of the site in a virtual directory under the root. My two sites needed to work together because of claims based auth shared between the two apps, but I was having a heck of a time getting Visual Studio IIS Express integration to work properly and consistently between the two projects. For me, the recipe to getting it working was as follows:

  1. 更新两个配置,以使应用程序在相同的主机和端口上运行,但在不同的目录中运行.一个目录可以是另一个目录的父目录,也可以是并行目录.两个应用程序必须处于同一解决方案中(这就是我们在这里关注的全部原因,对吧?).这可能需要更改launchSettings.json和/或.csproj文件,具体取决于您所使用的.Net版本,因为在创建项目时,Visual Studio会随机分配端口,因此无法更改通过用户界面.为了确保主机和端口在更改后正确无误,您可以通过查看解决方案资源管理器中项目的属性"元素来检查设置,具体取决于.Net版本.在Web或调试"选项卡中,都可以.

  1. Update both configs for the apps to run on the same host and port but in different directories. One directory may be a parent of the other, or they may be parallel. Both apps must be in the same solution (which is the whole reason we care here, right?). This may entail altering the launchSettings.json and/or the .csproj file, depending on what version of .Net you're using since ports are otherwise assigned randomly by Visual Studio when creating a project, and there isn't a way to change it via the UI. To make sure the hosts and ports are correct after changing them, you can inspect the settings by viewing the project 'Properties' element in Solution Explorer, in the Web or Debug tabs, depending on .Net version.

请确保该解决方案的任何.Net 5/Core Web应用程序的根目录中都有一个web.config文件.这是确保设置不会发生的关键.不能在applicationHost.config中获取坚果,而这正是我所缺少的部分.默认的Visual Studio行为和默认的.Net 5 Web应用程序模板将在applicationHost.config中导致类似这样的情况,这是在同时运行多个应用程序时出现的问题:

Make sure there is a web.config file in the root of any .Net 5 / Core web app that is part of the solution. This is key in making sure settings won't get nutty in the applicationHost.config, and this was the part I was missing. The default Visual Studio behavior and default .Net 5 web app templates will result in something like this in the applicationHost.config, and this is the problem when running multiple apps together:

产生问题的applicationHost.config片段:

Problematic resulting applicationHost.config snippet:

<aspNetCore processPath="%LAUNCHER_PATH%" stdoutLogEnabled="false" hostingModel="InProcess" startupTimeLimit="3600" requestTimeout="23:00:00" />

这里的问题是%LAUNCHER_PATH%不一致,具体取决于您启动的应用程序.为了避免将类似的配置添加到applicationHost.config中,您可以将web.config添加到每个.Net Core/5应用程序的根目录,如下所示:

The problem here is %LAUNCHER_PATH% isn't consistent, depending on which app you start. To keep config like that from being added to the applicationHost.config, you can add a web.config to the root of each .Net Core/5 app similar to this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <remove name="aspNetCore"/>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="bin\Debug\net5.0\YourExeNameHere.exe" arguments="" stdoutLogEnabled="false" hostingModel="InProcess">
        <environmentVariables>
          <environmentVariable name="ASPNETCORE_HTTPS_PORT" value="44300" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>

您的配置可能会有所不同.此处的关键只是将%LAUNCHER_PATH%替换为本地web.config中exe的路径,无论如何启动这些应用程序或这些应用程序之间如何关联,IIS Express都会使用该路径.您可能需要使用OutOfProcess HostingModel,和/或可能需要指定其他一些参数,例如HTTPS端口.您可能需要调整applicationHost.config,以清除可能遗留在其中的任何以前的混乱情况.删除并让VS重新创建applicationHost.config甚至可能会有所帮助.

Your config may vary. The key here is just to replace %LAUNCHER_PATH% with a path to the exe in the local web.config that IIS Express will pick up no matter how the apps are launched or how the apps are related to one another. You may need to use OutOfProcess hostingModel, and/or you may need to specify some other parameters like the HTTPS port. You may need to adjust the applicationHost.config to clean up any prior mess that may have been left in it. Deleting and letting VS recreate the applicationHost.config may even help.

如果继续出现错误,请尝试通过系统托盘停止IIS Express,然后重新启动它.尽管能够在IIS Express中同时使用所有应用程序,但是从另一应用程序的角度来看,IIS Express忙于为解决方案提供服务时,您仍然无法为其中一个应用程序在浏览器中查看".这还具有不幸的副作用,即在服务应用程序时锁定输出.exe,因此可能经常需要终止IIS Express才能修复生成错误.根据您的特定情况,您可能还需要更改一些其他applicationHost.config设置,以允许覆盖本地web.config文件中的设置.例如OverrideModeDefault ="允许";和/或lockItem ="false".

If you continue to get errors, try stopping IIS Express via your system tray and restart it. In spite of being able to use all the apps at the same time in IIS Express, you still cannot 'View in Browser' for one of the apps while IIS Express is busy serving the solution from the standpoint of the one of the other apps. This also has the unfortunate side effect of locking the output .exe while apps are being served, so killing IIS Express may need to be a regular occurrence to fix build errors. Depending on your particular situation, you may also need to change some other applicationHost.config settings to allow overriding settings in local web.config files. e.g. overrideModeDefault="Allow" and/or lockItem="false".

这篇关于在同一主机和端口下运行多个应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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