wcf类实现多个服务契约

时间:2010-05-12 08:29:19

标签: c# wcf multiple-inheritance servicecontract

我有一个类TestService,它实现了两个名为IService1IService2的服务合同。 但我面临着实施上的困难。 我的代码如下:

Uri baseAddress = new Uri("http://localhost:8000/ServiceModel/Service");
Uri baseAddress1 = new Uri("http://localhost:8080/ServiceModel/Service1");

ServiceHost selfHost = new ServiceHost(typeof(TestService));

selfHost.AddServiceEndpoint(typeof(IService1), new WSHttpBinding(), baseAddress);
selfHost.AddServiceEndpoint(typeof(IService2), new WSHttpBinding(), baseAddress1);

ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);

selfHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();

selfHost.Close();

我遇到运行时错误:

  

HttpGetEnabled属性   ServiceMetadataBehavior设置为true   和HttpGetUrl属性是一个   相对地址,但没有http   基地址。要么提供http   基地址或将HttpGetUrl设置为   绝对地址。

我该怎么办?我真的需要两个独立的端点吗? 感谢。

2 个答案:

答案 0 :(得分:2)

你可以用两种方式修复它

1)

Uri baseAddress = new Uri("http://localhost:8000/ServiceModel");
ServiceHost selfHost = new ServiceHost(typeof(TestService), baseAdress);

selfHost.AddServiceEndpoint(typeof(IService1), new WSHttpBinding(), "Service");
selfHost.AddServiceEndpoint(typeof(IService2), new WSHttpBinding(), "Service1");

ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);

2)

ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.HttpGetUrl = new Uri("http://localhost:8000/ServiceModel");
selfHost.Description.Behaviors.Add(smb);

答案 1 :(得分:1)

您需要做的就是添加一个基地址。 你还有两个独立的端点。

ServiceHost selfHost = new ServiceHost(typeof(TestService), new Uri ("http://localhost:8080/ServiceModel"));