Search code examples
c#wcfmultiple-inheritanceservicecontract

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 to an absolute address.

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


Solution

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