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();
이것 좀보세요 : http://stackoverflow.com/a/16501801/594832 – khlr
내 편집 된 질문보기. – user3343396
MEX (MetadataExchange) 끝점을 추가해야합니다. http://msdn.microsoft.com/en-us/library/aa738489%28v=vs.110%29.aspx – khlr