Search code examples
c#smbhttplistener

C# HttpListener Class not Working with Windows 10 version 1803


I am installing software on a Windows 10 machine with a fresh install of update 1803 which uses the C# HttpListener class as web-server. This software has worked on every version of Windows since XP. However on 1803, I cannot connect to the web server suddenly. If I run the software as an Administrator (which I have never had to do before), I can connect on localhost only, but not from another machine on the network.

I have been through all the articles on networking with version 1803 and sharing and turning on SMB version 1, but none of that has handled the problem. I am baffled at the moment.

This is the server code:

internal WebServer(RuntimeDB database)
{
    _db = database;
    ThreadPool.SetMaxThreads(50, 1000);
    ThreadPool.SetMinThreads(5, 50);
    _listener = new HttpListener {IgnoreWriteExceptions = true};
    _listener.Prefixes.Add("https://+:443/secure/");
    _listener.Prefixes.Add("http://+:80/trigger/");
}

internal void Start()
{
    try {
        _listener.Start();
        while (_listener.IsListening) {
            try {
                var request = _listener.GetContext();
                ThreadPool.QueueUserWorkItem(ProcessRequest, request);
            } catch (HttpListenerException) {
                break;
            } catch (InvalidOperationException) {
                break;
            }
        }
    } catch (Exception e) {
        LogMsg($"Fatal Error on thread WebServer: {e.Message}. Source: {e.Source} Target Site: {e.TargetSite}  Stack Trace: {e.StackTrace}");
    } finally {
        _listener?.Stop();
        _listener?.Close();
    }
}

This works fine on the previous version of Windows 10, so I am assuming there is some change in the networking stack or something. No exceptions are being generated by the code. Any clues are appreciated.


Solution

  • You have to enable listening on the specific ports by using the netsh-command.

    netsh add urlacl url=http://+:80/ user=DOMAIN\user listen=yes
    

    See this for more info