2017-03-01 75 views
0

以下使用Autofac和Nancy結合使用的程序不能正確啓動默認的Nancy服務器。Autofac和Nancy

using Autofac; 
using Nancy.Hosting.Self; 
using System; 

namespace NancyExample 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var builder = new ContainerBuilder(); 
      builder.Register(c => new NancyHost(new Uri("http://localhost:8080"))).SingleInstance(); 

      using (var container = builder.Build()) 
      { 
       NancyHost host = container.Resolve<NancyHost>(); 

       // this fails with: 
       // Exception thrown: 'System.Net.HttpListenerException' in System.dll 
       // Exception thrown: 'System.Net.HttpListenerException' in System.dll 
       // Exception thrown: 'System.InvalidOperationException' in System.dll 
       // Exception thrown: 'System.InvalidOperationException' in System.dll 

       // this works: 
       // NancyHost host = new NancyHost(new Uri("http://localhost:8080")); 

       host.Start(); 
      } 

      Console.ReadLine(); 
     } 
    } 
} 

當通過Autofac解決NancyHost,似乎一個錯誤出現內.NET的HttpListener深。這個例外似乎沒有很好的細節。訪問http://localhost:8080導致無法連接。

實例化NancyHost自己工作正常。

使用:

  • Autofac 4.3
  • 南希1.4.3
  • Nancy.Hosting.Self 1.4.1
+0

爲什麼要從容器中解析'NancyHost'?它根本不使用容器? – khellang

+0

另外,我不確定爲什麼這個調用會失敗,並且與'HttpListener'有關。在調用'Start'之前,Nancy甚至沒有觸及'HttpListener'。 – khellang

+0

我簡化了用例來證明問題。實際上我會從容器注入配置。我列出的錯誤實際上是在調用Start時。 –

回答

1

因爲你的代碼 「正在等待」 關於Console.ReadLine();,它是在using之外,Autofac容器已經處理完畢。在using之內移動Console.ReadLine();以使其工作。

+0

我真傻!非常感謝。 –