WCF: How to actually get to use WSHttpBinding? I get exceptions instead

michael picture michael · Mar 14, 2011 · Viewed 10.6k times · Source

I created a WCF Service:

Shared.dll:

[ServiceContract(ConfigurationName = "ICalculator")]
public interface ICalculator
{
    [OperationContract()]
    int Add(int a, int b);
}

Server:

[ServiceBehavior()]
public class Calculator : ICalculator
{
    public int Add(int a, int b) { return a + b; }
}

Client (Attempt #1):

public class CalculatorClient : ClientBase<ICalculator>, ICalculator
{
    private static Binding binding = new WSHttpBinding("MyConfig");
    private static EndpointAddress remoteAddress = new EndpointAddress(...);

    public CalculatorClient() : base(binding, remoteAddress) { }

    public int Add(int a, int b)
    {
        return Channel.Add(a, b); //Exception
    }
}

Client (Attempt #2): -- Note: I added a Service Reference instead of creating a CalculatorClient myself (.NET created it for me).

static void Main(string[] args)
{
    Binding binding = new WSHttpBinding("MyConfig");
    EndpointAddress remoteAddress = new EndpointAddress(...);
    CalculatorClient client = new CalculatorClient(binding, remoteAddress);
    int result = client.Add(5, 4); //Exception
}

Client (Attempt #3): -- I changed it to be a BasicHttpBinding() instead

static void Main(string[] args)
{
    Binding binding = new BasicHttpBinding("MyConfig");
    EndpointAddress remoteAddress = new EndpointAddress(...);
    CalculatorClient client = new CalculatorClient(binding, remoteAddress);
    int result = client.Add(5, 4); //This works!
}

app.config:

<system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="MyConfig" /> <!-- did not add anything to this yet -->
      </wsHttpBinding>
    </bindings>
</system.serviceModel>


The exception I get is: Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost/CalculatorService.svc. The client and service bindings may be mismatched. I don't see how they can be mismatched when I use a shared dll file between my server and client. The BasicHttpBinding works, just not the WSHttpBinding (I haven't even attempted WS2007HttpBinding.


Exception: [System.ServiceModel.ProtocolException] {"Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost/CalculatorService.svc. The client and service bindings may be mismatched."} Inner Exception: [System.Net.WebException] 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'..

Answer

David H picture David H · Mar 14, 2011

You need to set the security to be used on the WSHttpBinding

http://msdn.microsoft.com/en-us/library/ms731884(v=VS.90).aspx

Updated with Sample Client/Server WSHttpBinding, default security

Client


    class Program
    {
        static void Main(string[] args)
        {
          var calcClient = new CalcClient();
          int i = 1;
          int j = 2;
          Console.WriteLine("Result of Adding {0} and {1} is {2}", i, j, calcClient.Add(i, j));
          Console.ReadKey();
        }
    }

    public class CalcClient : ICalculator
    {
        public CalcClient()
        {
            CalcProxy = ChannelFactory.CreateChannel(new WSHttpBinding(), new EndpointAddress("http://localhost:5050/CalcServer"));
        }

        ICalculator CalcProxy { get; set; }

        public int Add(int a, int b)
        {
            return CalcProxy.Add(a, b);
        }
    }

Server


class Program
    {
        static void Main(string[] args)
        {
            var host = new ServiceHost(typeof (CalcSvr));
            host.AddServiceEndpoint(typeof (ICalculator), new WSHttpBinding(), "http://localhost:5050/CalcServer");
            host.Open();
            Console.WriteLine("Server Running");
            Console.ReadKey();
        }
    }