Search code examples
wcfwcf-binding

WCF Custom endpoints - Affecting Bindings?


I have a Self-Hosted WCF Service and client.
The client does not have a service reference, I have linked it to the endpoint programmatically.

The bindings are set to BasicHttpBinding in both the client and service -

Service

Uri baseAddress = new Uri("http://localhost:8733/Design_Time_Addresses/DSCentralService/Service1/");

DSCentralService.Service1 contentServer = new DSCentralService.Service1();

//initialise the servicehost
centralSvrHost = new ServiceHost(typeof(DSCentralService.Service1), baseAddress);

//add bindings
centralSvrHost.AddServiceEndpoint(
      typeof(DSCentralService.IService1), 
      new BasicHttpBinding(), 
      baseAddress
);

Client

serviceFactory = new ServiceFactory<DSCentralService.IService1>();

String serviceAddress="http://localhost:8733/Design_Time_Addresses/DSCentralService/Service1/";

iContentServer = serviceFactory.GetService(serviceAddress);

Service Factory Class

public class ServiceFactory<T> where T : class
{
    private T _service;

    public T GetService(string address)
    {
        return _service ?? (_service = GetServiceInstance(address));
    }

    private static T GetServiceInstance(string address)
    {               
        BasicHttpBinding basicBinding = new BasicHttpBinding();
        basicBinding.Name = "DSCentralSvr";
        basicBinding.TransferMode = TransferMode.Streamed;
        basicBinding.MessageEncoding = WSMessageEncoding.Mtom;
        basicBinding.MaxReceivedMessageSize = 10067108864;
        basicBinding.SendTimeout = new TimeSpan(0, 10, 0);
        basicBinding.OpenTimeout = new TimeSpan(0, 10, 0);
        basicBinding.CloseTimeout = new TimeSpan(0, 10, 0);
        basicBinding.ReceiveTimeout = new TimeSpan(0, 10, 0);

        EndpointAddress endpoint = new EndpointAddress(address);

        return ChannelFactory<T>.CreateChannel(basicBinding, endpoint);
    }
}

Yet upon debugging, I receive the common error of

The client and service bindings may be mismatched

There are no settings for bindings in any config files of either the client or service, to avoid conflicts with the programmatic settings.

Is there something I have missed, which is necessary when doing this programmatically? What is causing this mis-match?


Solution

  • You are hosting the service with a default BasicHttpBinding which means TransferMode Buffered and MessageEncoding Text.

    In your client you are using Streamed and Mtom, respectively.