2

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'..

michael
  • 14,844
  • 28
  • 89
  • 177
  • Would you mind posting the complete exception? – John Saunders Mar 14 '11 at 18:55
  • What is the service configuration? – Johann Blais Mar 14 '11 at 19:11
  • Where are you hosting your service? Do you have a WSHttpBinding configured for the service? – Zann Anderson Mar 14 '11 at 19:12
  • What does your **server** config look like? The `ProtocolException` would almost indicate you have REST endpoint but try to call it with SOAP - or your call results in a HTML error page. In your service project - can you right-click on the *.svc file and do a "View in browser" ?? As long as that doesn't even work, you're totally out of luck.... – marc_s Mar 14 '11 at 19:57
  • @marc_s: If i type in http://localhost/CalculatorService.svc I get the standard "You have created a service." page. I haven't touched my web.config file, so if something needs to be updated in there to make my server https compliant, i'd love to know what. :) – michael Mar 14 '11 at 20:09

1 Answers1

2

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();
        }
    }
David H
  • 366
  • 2
  • 8
  • Any advice on how to setup the server to match up with the security? When I choose a security mode (transport), it expects me to use `https` instead of `http`, which simply changing the uri doesn't meet that requirement? – michael Mar 14 '11 at 19:15
  • Transport is going to require SSL – David H Mar 15 '11 at 14:29