weixin_39722921
weixin_39722921
2021-01-07 01:49

Kestrel on Ubuntu not complying to UseUrls("http://*")

Sorry if this is the wrong place to post this issue.

I have the following code. The UseUrls below works fine on Windows. However when i run it on Ubuntu Kestrel continues to listen only on localhost:5000 (note, 5000 and not 50000) as defined below. I would expect it to listen on localhost:50000 as well as externalIP:50000


public static void Main(string[] args)
{
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseUrls("http://*:50000")
                .UseContentRoot(Directory.GetCurrentDirectory())
                //.UseIISIntegration()
                .UseStartup<startup>()                
                .Build();

            host.Run();
}
</startup>

该提问来源于开源项目:aspnet/KestrelHttpServer

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

21条回答

  • weixin_39528843 weixin_39528843 4月前

    Sounds like you're editing one version and compiling another. When in doubt add a compiler error to Main and it will become pretty clear if your code is even being compiled.

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

    I'm running the exact same code on Windows to very different effect. On Windows, as expected i get "hello world" and no webserver started. On Linux, it is as if it is ignoring what's in my main. The biggest difference between Windows and Linux i can see is that i publish a self-contained app to Linux but on windows I'm running it as a portable app. No source code difference between the two.

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

    If you want to prove you're running what you think you are, use a decompiler and look at the binary. Maybe the build is failing and you're just running the old bits.

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

    Ok, i decompiled. The publish in VS is ignoring the code i write in Main.cs if i publish to Ubuntu (16.04-x64). It seems to input there some template code. If I publish to Windows, it takes my code instead of the template code. Same source code on my side. Deleting binaries and starting from scratch every time.

    Publish to Ubuntu is not emitting IL that represents my code. It's emitting some basic template IL

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

    Do you have a runtimes section in your project.json?

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

    Yes - , "runtimes": { "win8-x64": {}, "ubuntu.16.04-x64": {} },

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

    In fact, It doesn't emit my changes to Startup.Configure either (according to decompiler). But it does emit correct IL for Startup.ConfigureServices.

    This also explains this other problem I've had that i posted about here on StackOverflow

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

    Well, that's my problem! If i copy the published to Windows version (just that one dll that holds main.cs) over to my linux machine, it responds to the UseUrls and my console says "Now listening on http://*:50000 and listening to multiple IPs

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

    I guess based on what i'm saying (if you believe me! :)), this probably isn't the right forum for this. Would you guys happen to know what the right forum would be?

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

    dotnet cli? https://github.com/dotnet/cli

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

    To update everyone that helped me. I had to publish via command line instead of VS and things work now.

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

    https://github.com/aspnet/Tooling-Internal/issues/599

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

    What does "not complying" mean. What error do you get?

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

    I mean it doesn't obey the URL i give it. Instead it continues to only listen on localhost:5000. I don't get an error as far as i can tell. My app works fine. However I'm expecting it to bind to other interfaces then just localhost. That's the behavior I get on Windows just not on Ubuntu

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

    Do you have a firewall, router, or anything else like that that could be preventing the request from reaching the external IP address? Are you able to reach other servers running in the same machine?

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

    Two things make me think its not a router/firewall thing. First, it is still listening on the wrong port and not on the port i gave it. For example localhost:50000 (fifty thousand) doesn't work. Instead its listening on port 5000 (5 thousand) - even though i explicitly asked it to listen to 50,000. Secondly, from the local ubuntu machine, I am unable to access it from its 10.x.x.x IP. Of course externally also it doesn't work. Its basically ignoring the UseUrls

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

    When you start it, is Kestrel printing Now listening on: http://*:50000? Or is it printing Now listening on: http://localhost:5000?

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

    Assuming that it's printing Now listening on: http://localhost:5000 on linux you could try changing host.Run() to the following:

     C#
    using (host)
    {
        Console.WriteLine("ASPNETCORE_URLS: {0}", Environment.GetEnvironmentVariable("ASPNETCORE_URLS"));
        Console.WriteLine("Pre-start addresses: {0}", string.Join(", ", host.ServerFeatures.Get<iserveraddressesfeature>().Addresses));
        host.Start();
        Console.WriteLine("Post-start addresses: {0}", string.Join(", ", host.ServerFeatures.Get<iserveraddressesfeature>().Addresses));
        Console.ReadLine();
    }
    </iserveraddressesfeature></iserveraddressesfeature>

    My guess is that its that the ASPNETCORE_URLS environment variable somehow got set.

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

    You can also store the WebHostBuilder object into a hostBuilder variable before calling .Build() and take a look at its "urls" setting. E.g: Console.WriteLine("urls: {0}", hostBuilder.GetSetting("urls"));

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

    I want to confirm that indeed it is printing Now listening on: http://localhost:5000

    I'll try some of the other stuff requested.

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

    Well, i must be going crazy now because even if i remove everything in Main(string[] args) and write whatever i want to the console, nothing changes. I get nothing new in the console. Kestrel still runs and i'm still able to use my app. See code below

    It seems to be when publishing to linux, asp .net core is assuming lots of things.

    Here is what i'm running. Notice all the commented out code. This is almost unbelievable, even though i've tried extensively to prove myself wrong.

    
    public static void Main(string[] args)
            {
                Console.WriteLine("Hello world!");
    
                //var host = new WebHostBuilder()
                //    .UseKestrel()
                //    .UseUrls("http://*:50000")
                //    .UseContentRoot(Directory.GetCurrentDirectory())
                //    //.UseIISIntegration()
                //    .UseStartup<startup>()                
                //    .Build();
    
    
                //Console.WriteLine("ASPNETCORE_URLS: {0}", Environment.GetEnvironmentVariable("ASPNETCORE_URLS"));
                //Console.WriteLine("Pre-start addresses: {0}", string.Join(", ", host.ServerFeatures.Get<iserveraddressesfeature>().Addresses));
                //host.Start();
                //Console.WriteLine("Post-start addresses: {0}", string.Join(", ", host.ServerFeatures.Get<iserveraddressesfeature>().Addresses));
                Console.ReadLine();
                //host.Run();
            }
    </iserveraddressesfeature></iserveraddressesfeature></startup>

    Basically everthing in main is commented out. Why does it still run anything!?!?

    点赞 评论 复制链接分享