WCF Interoperability with other services, content type is text/html

746 views Asked by At

I am trying to create a WCF client that operates with an http rest endpoint not based on Microsoft technologies. My WCF-foo is pretty weak, so I don't understand what I am doing incorrectly... I've created a service contract that looks like this...

[ServiceContract]
public interface IFilters
{
    [OperationContract]
    [WebGet(UriTemplate = "/api/filter.getavailable.xml?api_user={username}&api_key={password}")]
    String GetAvailableFilters(String username, String password);
}

Which I try and run like this...

    public String Run(String username, String password)
    {
        var binding = new BasicHttpBinding();
        binding.MessageEncoding = WSMessageEncoding.Text;
        binding.Security.Mode = BasicHttpSecurityMode.Transport;

        var endpointAddress = new EndpointAddress("https://sendgrid.com");

        IFilters proxy = ChannelFactory<IFilters>.CreateChannel(binding, endpointAddress);
        var result = "";
        using (proxy as IDisposable)
        {
            result = proxy.GetAvailableFilters(username, password);
        }
        return result;
    }

When I run this code, I get an exception that says...

The content type text/html; charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 1024 bytes of the response were: ...

Now if I just try and access this from a web browser (with different credentials), I get the xml doc I'm looking for... https://sendgrid.com/api/filter.getavailable.xml?api_user=foo&api_key=bar

what am I doing incorrectly?

Edit:

This was the final working solution...

    public Filters Run(String username, String password)
    {
        var binding = new WebHttpBinding(WebHttpSecurityMode.Transport);
        var endpointAddress = new EndpointAddress("https://sendgrid.com");
        var factory = new ChannelFactory<IFilters>(binding, endpointAddress);
        factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
        var proxy = factory.CreateChannel();
        using (proxy as IDisposable)
        {
            var results = proxy.GetAvailableFilters(username, password);
            return results;
        }
    }
1

There are 1 answers

0
Mike Goodwin On BEST ANSWER

On the client side you are using BasicHttpBinding which is a SOAP binding not a REST binding. You should try using the WebClient class instead

http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx

or WebChannelFactory

http://msdn.microsoft.com/en-us/library/bb908674.aspx