开发者

WCF class implementing multiple service contracts

I have a class TestService which implements two service contracts called IService1 and IService2. But I'm facing a difficulty in implementation.

My code looks as follows:

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();

I'm getting a run time error as:

The HttpGetEnabled property of ServiceMetadataBehavior is set to true and the HttpGetUrl property is a relative address, but there is no http base address. Either supply an http base address or set HttpGetUrl t开发者_开发知识库o an absolute address.

What can I do about it? Do I really need two separate endpoints?


you can fix it in two ways

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);


All you need to do is to add an base address. you still have two separated endpoints.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜