2015-01-16 8 views
0

WCF 서비스를 호스팅하는 콘솔 응용 프로그램이 있습니다. 지금은 다음과 같은 방법으로 실행되도록 응용 프로그램을 구성 : 다음 WCF 콘솔 응용 프로그램 (여러 서비스 포함)

// within my program class 
class Program 
{ 
    static void Main(string[] args) 
    { 
     // retrieve the current URL configuration 
     Uri baseAddress = new Uri(ConfigurationManager.AppSettings["Uri"]); 

내가 이제 제외

using (WebServiceHost host = new WebServiceHost(typeof(MonitorService), baseAddress)) 
{ 
    ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof (IMonitorService), new WebHttpBinding(), ""); 
    ServiceDebugBehavior stp = host.Description.Behaviors.Find<ServiceDebugBehavior>(); 
    stp.HttpHelpPageEnabled = false; 

    host.Open(); 

    Console.WriteLine("The service is ready at {0}", baseAddress); 
    Console.WriteLine("Press <Enter> to stop the service."); 
    Console.ReadLine(); 

    // Close the ServiceHost. 
    host.Close(); 
} 

지금까지 너무 좋아 내 WCF의 REST 서비스를 호스팅하는 WebServiceHost의 새로운 인스턴스를 시작 I 다음과 같은 구조에서 호스팅이 개 WCF 서비스를 가지고있는 요구 사항을 함께했다

http://localhost:[port]/MonitorServicehttp://localhost:[port]/ManagementService

새 서비스 엔드 포인트를 추가하고 다른 계약을 사용하여 두 엔드 포인트를 구별 할 수 있습니까? 그렇다면 두 계약의 구현은 WebServiceHost에서 사용하는 것과 같은 클래스 인 MonitorService에 있어야합니다.

답변

1

예, 단일 콘솔 응용 프로그램에서 여러 서비스를 호스팅 할 수 있습니다. 여러 서비스에 대해 여러 호스트를 만들 수 있습니다. 다음과 같은 일반적인 방법을 사용하여 특정 서비스에 대한 호스트를 시작할 수 있습니다.

/// <summary> 
    /// This method creates a service host for a given base address and service and interface type 
    /// </summary> 
    /// <typeparam name="T">Service type</typeparam> 
    /// <typeparam name="K">Service contract type</typeparam> 
    /// <param name="baseAddress">Base address of WCF service</param> 
    private static void StartServiceHost<T,K>(Uri baseAddress) where T:class 
    { 
     using (WebServiceHost host = new WebServiceHost(typeof(T), baseAddress)) 
     { 
      ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(K), new WebHttpBinding(), ""); 
      ServiceDebugBehavior stp = host.Description.Behaviors.Find<ServiceDebugBehavior>(); 
      stp.HttpHelpPageEnabled = false; 

      host.Open(); 

      Console.WriteLine("The service is ready at {0}", baseAddress); 
      Console.WriteLine("Press <Enter> to stop the service."); 
      Console.ReadLine(); 

      // Close the ServiceHost. 
      host.Close(); 
     } 
    } 
+0

그래서 사실은 내가, 서비스 당 하나씩 여러 서비스 호스트를 시작하고 콘솔 응용 프로그램이 종료 다운을 때 모두 함께 처리하는 메커니즘을 작성해야합니다. 감사 – Raffaeu

관련 문제