NancyFx / Nancy

Lightweight, low-ceremony, framework for building HTTP based services on .Net and Mono
http://nancyfx.org
MIT License
7.15k stars 1.47k forks source link

Cannot access a disposed object error when stopping NancyHost #2979

Open ghost opened 5 years ago

ghost commented 5 years ago

Description

I'm using NancyHost for self hosting. But whenever I try to Stop or Dispose the host, I get these two exceptions:

mscorlib: Cannot access a disposed object.
Object name: 'listener'.
  at System.Net.ListenerAsyncResult.GetContext () [0x0001c] in <748af814b14a45f7803e4fe5c9300d51>:0 
  at System.Net.HttpListener.EndGetContext (System.IAsyncResult asyncResult) [0x00099] in <748af814b14a45f7803e4fe5c9300d51>:0 
  at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction, System.Threading.Tasks.Task`1[TResult] promise, System.Boolean requiresSynchronization) [0x0000f] in <52405d1838e24bc5b300f5ebeee9f7c4>:0 
--- End of stack trace from previous location where exception was thrown ---

  at Nancy.Hosting.Self.NancyHost+<>c__DisplayClass14_0.<Start>b__1 (System.Threading.Tasks.Task`1[TResult] contextTask) [0x00087] in <5e6fd4328ff54d50a87c10c9ee6cd54a>:0
mscorlib: Cannot access a disposed object.
Object name: 'listener'.
  at System.Net.ListenerAsyncResult.GetContext () [0x0001c] in <748af814b14a45f7803e4fe5c9300d51>:0 
  at System.Net.HttpListener.EndGetContext (System.IAsyncResult asyncResult) [0x00099] in <748af814b14a45f7803e4fe5c9300d51>:0 
  at System.Threading.Tasks.TaskFactory`1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func`2[T,TResult] endFunction, System.Action`1[T] endAction, System.Threading.Tasks.Task`1[TResult] promise, System.Boolean requiresSynchronization) [0x0000f] in <52405d1838e24bc5b300f5ebeee9f7c4>:0 
--- End of stack trace from previous location where exception was thrown ---

  at Nancy.Hosting.Self.NancyHost+<>c__DisplayClass14_0.<Start>b__1 (System.Threading.Tasks.Task`1[TResult] contextTask) [0x00087] in <5e6fd4328ff54d50a87c10c9ee6cd54a>:0

Steps to Reproduce

The host function does this:

Task.Run(() =>
        {
            _host = new NancyHost(new HostConfiguration
            {
                UnhandledExceptionCallback = ExceptionHandler
            }, new Uri($"http://localhost:{port}"), new Uri($"http://127.0.0.1:{port}"));
            _host.Start();
            EmitSignal(nameof(OnStart)); // to notify the rest of code
        });

System Configuration