1

I have an autogenerated binding from a WSDL file in a Visual Studio project that looks like this:

<binding name="XServiceSoap" 
         maxBufferPoolSize="16777216"
         maxBufferSize="16777216" 
         maxReceivedMessageSize="16777216">
    <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Ntlm" 
                   proxyCredentialType="None"/>
    </security>
</binding>

and the client looks like this:

<endpoint address="http://uri/XService.asmx"
          binding="basicHttpBinding" 
          bindingConfiguration="XServiceSoap"
          contract="XService.XServiceSoap" 
          name="XServiceSoap"/>

In the code, I'm easily able to change the endpoint address (and credentials) using

using WPFApp.XService;
var xServiceSoapClient = new XServiceSoapClient("XServiceSoap");
xServiceSoapClient.Endpoint.Address = new EndpointAddress(NEW_ENDPOINT);
xServiceSoapClient
    .ClientCredentials.Windows.ClientCredential.UserName = NEW_USERNAME;
xServiceSoapClient
    .ClientCredentials.Windows.ClientCredential.Password = NEW_PASSWORD;

But if I try using a non-http endpoint, I got thrown an exception The provided URI scheme 'https' is invalid; expected 'http'.

This error makes sense to me (ex. from here), but I want the same binding to support both HTTP and HTTPS when I change the endpoint (test vs prod server). How do I do this in code with the built in soap client? I don't see an option in the xServiceSoapClient to change the security mode, ex. I don't see a XServiceSoapclient.Security.Mode option where it can be changed.

I can confirm when I change the app.config manually from TransportCredentialOnly to Transport, https connections work without throwing an exception.

Thank you!

EldHasp
  • 6,079
  • 2
  • 9
  • 24
Thomas
  • 555
  • 7
  • 29
  • You can refer to: https://stackoverflow.com/questions/2435823/the-provided-uri-scheme-https-is-invalid-expected-http-parameter-name-via – Theobald Du Jul 07 '21 at 07:25

1 Answers1

1

The constructor for the WCF client has an overload that takes two parameters, binding and endpoint

you can use this overload to pass your desired security mode:

WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.Transport;

var xServiceSoapClient = new xServiceSoapClient(binding, "XServiceSoap");

This is the general idea, using the designated object rather then the service client object that is not meant for the above kind of manipulations.

Barr J
  • 10,636
  • 1
  • 28
  • 46
  • 1
    Wanted to mention this was exactly what I needed, though I used `basicHttpBinding` instead of `WSHttpBinding` since there were some compatibility issues. – Thomas Jul 22 '21 at 23:17