Search code examples
wcfvisual-studio-2010service-reference

Unable to generate a proxy for a self-hosted service with "Add Service Reference"


I created a basic calculator service called MiniCalc that only has two operations. Add and Mul, and hosted it in a Console Application.

using(ServiceHost host = new ServiceHost(typeof(MiniCalcService.Service),
                                         new Uri("http://localhost:8091/MiniCalcService")))
{
    host.AddServiceEndpoint(typeof(MiniCalcService.IService),
                            new BasicHttpBinding(),
                            "Service");
    host.Open();
    Console.Write("Press ENTER key to terminate the MiniCalcHost . . . ");
}

Then I created a console application to consume the service and created the proxy manually by creating a proxy class and then created a ChannelFactory to invoke the service.

EndpointAddress ep = new EndpointAddress("http://localhost:8091/MiniCalcService/Service");
IService proxy = ChannelFactory<IService>.CreateChannel(new BasicHttpBinding(),ep);

I was able to invoke the service contract properly and retrieve the result as expected.

Now I wanted to create the proxy using the Add Service Reference.

I get the following error when I click Go in the Add Service Reference window

There was an error downloading 'http://localhost:8091/MiniCalcService/Service'.
The request failed with HTTP status 400: Bad Request.
Metadata contains a reference that cannot be resolved: 'http://localhost:8091/MiniCalcService/Service'.
Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:8091/MiniCalcService/Service.  The client and service bindings may be mismatched.
The remote server returned an error: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'..
If the service is defined in the current solution, try building the solution and adding the service reference again.

What am I missing or doing wrong?


Solution

  • Enable Metadata exchange behavior in your ServiceHost.

    using(ServiceHost host = new ServiceHost(typeof(MiniCalcService.Service),
                                 new Uri("http://localhost:8091/MiniCalcService")))
    {
        host.AddServiceEndpoint(typeof(MiniCalcService.IService),
                                new BasicHttpBinding(),
                                "Service");
    
        //Enable metadata exchange
        ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
        smb.HttpGetEnabled = true;
        host.Description.Behaviors.Add(smb);
    
        host.Open();
        Console.Write("Press ENTER key to terminate the MiniCalcHost . . . ");
    }
    

    http://wcftutorial.net/WCF-Self-Hosting.aspx