2017-03-01 1 views
0

다음 프로그램은 Nancy와 함께 Autofac을 사용하여 기본 Nancy 서버를 올바르게 시작하지 않습니다. Autofac를 통해 NancyHost를 해결하면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(); 
     } 
    } 
} 

, 오류가 깊은 .NET의 HTTP Listener에 내 온다 나타납니다. 이 예외에 대해서는 세부 사항이 좋지 않은 것 같습니다. http://localhost:8080을 방문하면 연결되지 않습니다.

NancyHost를 인스턴스화하는 것이 좋습니다.

사용 :

  • Autofac 4.3
  • 낸시가 Console.ReadLine();에 코드 "기다리고있다가"하고 Nancy.Hosting.Self 1.4.1
+0

왜 컨테이너에서 NancyHost를 해결하겠습니까? 컨테이너를 전혀 사용하지 않습니까? – khellang

+0

또한 왜 그 호출이'HttpListener'와 관련된 것에 실패 할 지 확신하지 못합니다. 낸시는'Start'를 호출하기 전에'HttpListener'에 손대지 않습니다. – khellang

+0

문제를 설명하기 위해 유스 케이스를 단순화했습니다. 실제로 컨테이너에서 구성을 주입합니다. 내가 열거 한 오류는 실제로 시작이 호출 될 때입니다. –

답변

1

  • 한다 1.4.3 때문에 using 외부에는 Autofac 컨테이너가 이미 처리되었습니다. Console.ReadLine();using 안에 넣으면 제대로 작동합니다.

  • +0

    어리석은 나를! 정말 고맙습니다. –