WCF in .net core (TransportWithMessageCredential)

2019-01-28 11:14发布

问题:

When I try to create a connection to a WCF client in dotnet core 2.0, I receive an platform unsupported error:

System.PlatformNotSupportedException: 'The value 'TransportWithMessageCredential' is not supported in this context for the binding security property 'securityMode'.'

If I remove the BasicHttpSecurityMode, I receive an argument exception: System.ArgumentException: 'The provided URI scheme 'https' is invalid; expected 'http'.'

Code:

ChannelFactory<BlackBoxContract> factory = null;
BlackBoxContract serviceProxy = null;
Binding binding = null;

binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);

factory = new ChannelFactory<BlackBoxContract>(binding, new EndpointAddress("https:......."));;
serviceProxy = factory.CreateChannel();

Anyone that found a workaround as this might be on the long term roadmap? https://github.com/dotnet/wcf/issues/8

回答1:

Actually found a valid workaround, there is a package you can use for this: https://github.com/gravity00/SimpleSOAPClient

using SimpleSOAPClient;
using SimpleSOAPClient.Handlers;
using SimpleSOAPClient.Helpers;
using SimpleSOAPClient.Models;
using SimpleSOAPClient.Models.Headers;

...

_client = SoapClient.Prepare().WithHandler(new DelegatingSoapHandler());
_client.HttpClient.DefaultRequestHeaders.Clear();
_client.HttpClient.DefaultRequestHeaders.Add("SOAPAction", "Action...");

 var requestEnvelope = SoapEnvelope
     .Prepare()
     .Body(request)
     .WithHeaders(KnownHeader.Oasis.Security.UsernameTokenAndPasswordText(Username, Password));

var responseEnvelope = _client.Send(Url, "CanNotBeEmpty", requestEnvelope);

Got it to work like this, as a charm...