weixin_39863017
weixin_39863017
2020-12-26 19:30

AspNetCore RC2: Kestrel server won't bind to multiple server.urls

The server is not honoring server url bindings when launching web apps using .Net Core RC2 VS tools, eg this doesn't work:


var host = new WebHostBuilder()
    .UseKestrel()
    .UseContentRoot(Directory.GetCurrentDirectory())
    .UseUrls("http://localhost:60000", "http://localhost:60002")
    .UseIISIntegration()
    .UseStartup<startup>()
    .Build();

host.Run();
</startup>

However, everything works fine when launching this way:

project-root > dotnet run

I've been digging around, and can't find much on this topic other than a few related issues: - https://github.com/aspnet/Announcements/issues/174 - http://stackoverflow.com/questions/34212765/how-do-i-get-the-kestrel-web-server-to-listen-to-non-localhost-requests

I tried moving the urls to a server.urls variable in an external hosting.json file and using the configuration builder as suggested in the 2nd link, but that's not working either.


var config = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("hosting.json", optional: true)
    .Build();

var host = new WebHostBuilder()
    .UseKestrel()
    .UseConfiguration(config)
    .UseContentRoot(Directory.GetCurrentDirectory())         
    .UseIISIntegration()
    .UseStartup<startup>()
    .Build();

host.Run();

//  contents of hosting.json, 
// {
//   "server.urls": "http://localhost:60000 http://localhost:60002"
// }
</startup>

The hosting.json file is located in the project root along side project.json and was created based on the only example I could find: - https://docs.asp.net/en/latest/fundamentals/servers.html)

Although seemlingly promising, that didn't work either.Are you having this issue too or has anyone else reported it?

I'm using 1.0.0-preview1-002702.

该提问来源于开源项目:saaskit/saaskit

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

5条回答

  • weixin_39863017 weixin_39863017 4月前

    Edited:

    I tried both.. using a custom application and with your samples, yet neither worked. It's not so much that the SaasKit isn't working, rather it's that in RC2 it's impossible to get IIS Express / Kestrel to listen on multiple ports to even test it.

    Meaning, in RC1 this worked:

    
    // project.json
    
    "commands": {
        "web": "Microsoft.AspNet.Server.Kestrel --server.urls=http://localhost:60000;http://localhost:60002"
    }
    

    (I could launch and debug multiple tenants listening on different ports)

    However, in RC2 this does not work:

    
    // program.cs
    
    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseUrls("http://localhost:60000", "http://localhost:60002")
            .UseIISIntegration()
            .UseStartup<startup>()
            .Build();
        host.Run();
    }
    </startup>

    (Only port 60000 works, none of the other specified ports are being listened on)

    Thus, I've came to the following conclusion: - Telling WebHost to .UseUrls(...) does absolutely nothing!

    To make sure it wasn't just my machine, I had 3 other developers clone your RC2 version of the SaasKit, and run it "as is" out of the box.

    All 3 had the exact same issue! The application launched port 60000 no problem, but attempts to browse to any of the other ports resulted in a "host cannot be reached" error ..... because nothing is listening there.

    If you pull the IServerAddressesFeature from the DI container, you'll see none of the urls are listed there either. Add the addresses to that instance, and the other ports will at least attempt to resolve before eventually failing a token pairing check in the AspNetCore IIS Middleware. Clone the IISMiddleware and comment out the token pairing check and it works.

    Am I missing something? Surely I don't need to hack the IIS Middleware to run / debug in VS 2015 using IIS Express, do I?.... because that's exactly what I'm stuck doing now.

    点赞 评论 复制链接分享
  • weixin_39863017 weixin_39863017 4月前

    I'm going to close this issue. It's not related to SaasKit, and it's easy enough to fix by doing the following: 1. Open the solution's \.vs\config\applicationhost.config file 2. Manually add the additional entries to the site's <bindings></bindings>

    Before

    
    <bindings>
       <binding protocol="http" bindinginformation="*:60000:localhost"></binding>
    </bindings>
    

    After

    
    <bindings>
       <binding protocol="http" bindinginformation="*:60000:localhost"></binding>
       <binding protocol="http" bindinginformation="*:60001:localhost"></binding>
       <binding protocol="http" bindinginformation="*:60002:localhost"></binding>
       <binding protocol="http" bindinginformation="*:60003:localhost"></binding>
       <binding protocol="http" bindinginformation="*:60004:localhost"></binding>
    </bindings>
    
    点赞 评论 复制链接分享
  • weixin_39714275 weixin_39714275 4月前

    It's an issue with IIS Express, and yes, changing ApplicationHost.config is the way to resolve it.

    点赞 评论 复制链接分享
  • weixin_39694838 weixin_39694838 4月前

    Are you running one of the samples or is this your own application? I did have this issue during the RC2 upgrade but just made the IIS Url in project properties the same as one of my configured URLs in Startup.cs e.g. http://localhost:60000.

    点赞 评论 复制链接分享
  • weixin_39694838 weixin_39694838 4月前

    I just tested this myself. You need to separate your URLs with a semi-colon:

    
    {
      "server.urls": "http://localhost:60000;http://localhost:60001"
    }
    
    点赞 评论 复制链接分享

相关推荐