Unable to consume WCF WSHttpBinding in .net core

Viewed 14395

I'am trying to move my project from .net to .net core. I was initially using WCF WSHttpBinding service in .net but I'am unable to consume the same in .net core. I tried using BasicHttpBinding to connect with WsHttpBinding on the client side, but it throws an error saying the Bindings should match on both client and server side.

Suggest how to implement WSHttpBinding on .Net Core without modifying WSHttpBindings on the client side.?

6 Answers

I was able to make it work using a custom binding. There are also https, text, and binary binding elements that can be configured.

var binding = new CustomBinding
{
    Elements = { new HttpTransportBindingElement //or HttpsTransportBindingElement for https:
    {
        MaxBufferSize = int.MaxValue,
        MaxReceivedMessageSize = int.MaxValue
    }}
};

WCF WSHttpBinding is not support yet in .net core 2.1. Here are the list of supported bindings in .Net core

  • BasicHttpBinding
  • CustomBinding
  • NetHttpBinding
  • NetTcpBinding

read more about supported features click here

I used WSHttpBinding on full framework. After migration to .net core 2.2 I faced with same problem.

Later I found that BasicHttpsBinding (it is https, not http) perfectly fits my need. I simple replaced WSHttpBinding with BasicHttpsBinding and all works fine (on windows and on linux).

This is a more complete answer and may help someone who is trying to call a SOAP service endpoint using .NET Core (.NET Core 2.2) and is struggling to get it working.

For the answer I'm assuming you've already added a Connected Service. If not, please refer to this Microsoft Article

The example below is using a BasicHttpBinding with an Ntlm Credential Type (DOMAIN\Username):

public async Task<string> ImportSalesOrder(string jsonString)
{
    var binding = new BasicHttpsBinding();
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;

    var endpoint = new EndpointAddress(_appConfig["endpoint"]);

    var client = new eCommIntegrationMgt_PortClient(binding, endpoint);

    client.ClientCredentials.UserName.UserName = _appConfig["usernam"];
    client.ClientCredentials.UserName.Password = _appConfig["password"];

    try
    {
        var result = await client.ImportSalesOrderAsync(jsonString);

        return result.return_value;
    }
    catch (Exception)
    {
        throw;
    }
}

_appConfig is a global variable, which is made available via DI (Dependency Injection). You can replace them with hard-coded values if you're not making use of DI. The catch is redundant here, but you can add your custom error handling/logging.

eCommIntegrationMgt_PortClient is the client i.e. the Service object where all our endpoints exist.

Here is snippet what I used

        var binding = GetCustomBinding();

        private Binding GetCustomBinding()
        {
            CustomBinding result = new CustomBinding();
            TextMessageEncodingBindingElement textBindingElement = new TextMessageEncodingBindingElement();
            result.Elements.Add(textBindingElement);
            HttpsTransportBindingElement httpsBindingElement = new HttpsTransportBindingElement
            {
                AllowCookies = true,
                MaxBufferSize = int.MaxValue,
                MaxReceivedMessageSize = int.MaxValue,
                AuthenticationScheme = System.Net.AuthenticationSchemes.Negotiate
            };
            result.Elements.Add(httpsBindingElement);
            return result;
        }
Related