2014-12-30 2 views
0

2 개의 서비스 계약을 구현하고 다른 포트에 2 개의 엔드 포인트를 표시하는 클래스를 만들고 싶습니다. (포트를 프로그램의 입력으로 가져옵니다).하나의 클래스가 두 개의 서비스 계약을 구현합니다.

내 질문은 this question과 매우 유사하지만 두 개의 종점이 각각 다른 포트에 있어야합니다.

편집 :

var aConnectionString = "http://localhost:8200/A"; 
var bConnectionString = "http://localhost:8201/B"; 

ServiceHost host= new ServiceHost(typeof(abImp)); 

var binding = new BasicHttpBinding(); 

host.AddServiceEndpoint(typeof(A), binding, aConnectionString); 
host.AddServiceEndpoint(typeof(B), binding, bConnectionString); 

{ 
    // Check to see if the service host already has a ServiceMetadataBehavior 
    ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>(); 

    // If not, add one 
    if (smb == null) 
     smb = new ServiceMetadataBehavior(); 

    smb.HttpGetEnabled = true; 
    smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; 
    host.Description.Behaviors.Add(smb); 


    host.AddServiceEndpoint(
     ServiceMetadataBehavior.MexContractName, 
     MetadataExchangeBindings.CreateMexHttpBinding(), 
     aConnectionString 
     ); 

} 
host.Open(); 

나는() 오퍼레이션 host.Open에이 오류 메시지를 받고 있어요 :

이 코드를 사용하여

An unhandled exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.dll 

Additional information: A binding instance has already been associated to listen URI 'http://localhost:8200/A'. If two endpoints want to share the same ListenUri, they must also share the same binding object instance. The two conflicting endpoints were either specified in AddServiceEndpoint() calls, in a config file, or a combination of AddServiceEndpoint() and config. 

솔루션을

  var aConnectionString = "http://localhost:" + portA+ "/A"; 
      var bConnectionString = "http://localhost:" + portB+ "/B"; 


      ServiceHost aHost = new ServiceHost(instance, new Uri(aConnectionString)); 
      ServiceHost bHost = new ServiceHost(instance, new Uri(bConnectionString)); 

      aHost.Description.Behaviors.Find<ServiceBehaviorAttribute>().InstanceContextMode = InstanceContextMode.Single; 
      bHost.Description.Behaviors.Find<ServiceBehaviorAttribute>().InstanceContextMode = InstanceContextMode.Single; 

      aHost.AddServiceEndpoint(typeof(A), new BasicHttpBinding(), aConnectionString); 
      bHost.AddServiceEndpoint(typeof(B), new BasicHttpBinding(), bConnectionString); 


      { 
       // Check to see if the service host already has a ServiceMetadataBehavior 
       var smb = aHost.Description.Behaviors.Find<ServiceMetadataBehavior>() ?? new ServiceMetadataBehavior(); 
       aHost.Description.Behaviors.Add(smb); 

       aHost.AddServiceEndpoint(
        ServiceMetadataBehavior.MexContractName, 
        MetadataExchangeBindings.CreateMexHttpBinding(), 
        aConnectionString + "/Mex" 
        ); 
      } 
      { 
       // Check to see if the service host already has a ServiceMetadataBehavior 
       var smb = bHost.Description.Behaviors.Find<ServiceMetadataBehavior>() ?? new ServiceMetadataBehavior(); 
       bHost.Description.Behaviors.Add(smb); 

       bHost.AddServiceEndpoint(
        ServiceMetadataBehavior.MexContractName, 
        MetadataExchangeBindings.CreateMexHttpBinding(), 
        bConnectionString + "/Mex" 
        ); 
      } 

      aHost.Open(); 
      bHost.Open(); 
+0

이것 좀보세요 : http://stackoverflow.com/a/16501801/594832 – khlr

+0

내 편집 된 질문보기. – user3343396

+0

MEX (MetadataExchange) 끝점을 추가해야합니다. http://msdn.microsoft.com/en-us/library/aa738489%28v=vs.110%29.aspx – khlr

답변

0

주소가 완전히 다른 두 개의 주소를 원한다면 같은 구현 클래스인데 두 가지ServiceHost 인스턴스를 사용하는 것이 좋습니다.

+0

답장을 보내 주셔서 감사합니다. 하지만, 제 질문에 쓴 것처럼, 저는이 두 서비스가 다른 포트에 필요합니다. – user3343396

+0

@ user3343396 분명히 완전히 다른 URI에 서비스를 원한다면 두 개의 다른 'ServiceHost'를 사용하지 않는 것이 좋습니다. 당신은 여전히 ​​동일한 구현 클래스를 원한다. 왜 오직 하나의 'ServiceHost' 인스턴스만으로이 작업을 수행하고자하는 이유가 있습니까? –

+0

왜냐하면 나는 구체적인 클래스가 싱글 톤이되기를 원하기 때문이다. – user3343396

관련 문제