aspnet / Hosting

[Archived] Code for hosting and starting up an ASP.NET Core application. Project moved to https://github.com/aspnet/Extensions and https://github.com/aspnet/AspNetCore
Apache License 2.0
552 stars 312 forks source link

IServerAddressesFeature doesn't work correctly #1491

Closed 0Lucifer0 closed 5 years ago

0Lucifer0 commented 6 years ago

Hello, it seems the ServerFeatures.Set doesn't work correctly. this is a code sample to test this issue

 using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace BugTest
{
    class BugTest
    {
        public static void MainBug()
        {
            var host = BuildWebHost(null);
            host.ServerFeatures.Set<IServerAddressesFeature>(new ServerAddressesFeature { Addresses = { "http://+:1337" }, PreferHostingUrls = true });
            host.Run(); // Doesn't work - logger say it use port 1337 but netstat show port 5000
        }

        public static void Main()
        {
            var host = BuildWebHost(null);
            var serveraddress = host.ServerFeatures[typeof(ServerAddressesFeature)] as ServerAddressesFeature;
            serveraddress.Addresses.Add("http://+:1337");
            serveraddress.PreferHostingUrls = true;
            host.Run(); //work - logger say it use port 1337 and netstat show port 1337
        }

        private static IWebHost BuildWebHost(string[] args)
        {
            return WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();
        }
    }

    public class Startup
    {
        // Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app)
        {

        }
    }
}
Tratcher commented 6 years ago

This is an ordering and ownership issue. The server creates the IServerAddressesFeature instance and maintains a reference to it. https://github.com/aspnet/KestrelHttpServer/blob/6551eae321cd6306b485906ea47e8d41e932d65b/src/Kestrel.Core/KestrelServer.cs#L58 https://github.com/aspnet/HttpSysServer/blob/91c518e13d7ede44e6cb88df895d9a242b07726e/src/Microsoft.AspNetCore.Server.HttpSys/MessagePump.cs#L56 If you provide your own implementation then the server doesn't know about it, it's still looking at the old one.

While possibly un-expected, it is working as intended. Do you have a specific reason for needing to set the feature?

0Lucifer0 commented 6 years ago

Not really a specific reason and the 2nd solution (main) work great. But still seems like a bug because the logger use the new one so the logger say listening on port 1337 even if port is 5000. My initial goal was to try to change the port in the startup and not in the UseUrls

erwan-joly commented 6 years ago

https://github.com/aspnet/Hosting/blob/f9d145887773e0c650e66165e0c61886153bcc0b/src/Microsoft.AspNetCore.Hosting/WebHostExtensions.cs make the console log to return something else than what we are really listening in this example

aspnet-hello commented 5 years ago

We periodically close 'discussion' issues that have not been updated in a long period of time.

We apologize if this causes any inconvenience. We ask that if you are still encountering an issue, please log a new issue with updated information and we will investigate.