Running multiple apps under same host and port

会有一股神秘感。 提交于 2020-11-29 02:53:18

问题


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

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.

Is there a way to configure it?

{
  "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"
    }
  }
}

回答1:


This is not possible, and is a perfect example of why any would be web developer should familiarize themselves with the actual platform they intend to develop on, namely: The HTTP and TCP/IP protocols, and frankly just how sockets work in general, in this case.

Communication between computers happens over ports via various protocols. That communication happens via packets, which are analogous to a letter sent in the mail. Just as with a letter, the packet is addressed, which allows the receiving computer to route it to the correct destination. When it gets to the ultimate destination computer, that computer must also route it to the application or appliance that it's directed to. This part is analogous to mail delivered to a business. All the mail is delivered to some central part of the organization, where it is then routed to the particular employee it's destined for.

With something like a web application, the "employee" in the above scenario is identified by the URL and/or port. Technically it's always the URL and port, but either can be a "default". So just a port assumes 0.0.0.0 or the set of all IP addresses assigned to the machine, and just a URL assumes the default protocol port (80 for HTTP, 443 for HTTPS). Regardless, there must be one unique recipient for that combination. Again, going back to the office analogy, imagine two employees where sharing and office and a piece of mail was addressed just to the room number of the office. How would you know which of the two employees it was actually destined for? You cannot. In actuality, it's more like two employees with the same first and last name were sharing an office. Even if you had the name of the employee, you still wouldn't know which to deliver it to.

In short, what you're wanting is not possible, and it's obviously not possible if you actually understand the platform you're developing on, which is frankly a prerequisite for any sort of development. Each application must use a unique URL, a unique port, or both. There is no other choice.




回答2:


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. 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.

  2. 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:

Problematic resulting applicationHost.config snippet:

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

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>

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.

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".



来源:https://stackoverflow.com/questions/55396337/running-multiple-apps-under-same-host-and-port

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!