0

我有一个类,它实现了两个名为and的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();

我收到一个运行时错误:

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

我能做些什么呢?我真的需要两个独立的端点吗?

4

2 回答 2

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);
于 2010-05-12T08:56:08.167 回答
1

您需要做的就是添加一个基地址。您仍然有两个单独的端点。

ServiceHost selfHost = new ServiceHost(typeof(TestService), new Uri ("http://localhost:8080/ServiceModel")); 
于 2010-05-12T08:55:00.460 回答