views:

26

answers:

2

I have a WCF service that I need to call. When I go through "add service reference" in VS, I get following client config:

<configuration>
    <system.serviceModel>
        <bindings>
            <customBinding>
                <binding name="CustomBinding_IMyService">
                    <binaryMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
                        maxSessionSize="2048">
                        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    </binaryMessageEncoding>
                    <httpTransport manualAddressing="false" maxBufferPoolSize="524288"
                        maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous"
                        bypassProxyOnLocal="false" decompressionEnabled="true" 
                        hostNameComparisonMode="StrongWildcard"
                        keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous"
                        realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
                        useDefaultWebProxy="true" />
                </binding>
            </customBinding>
        </bindings>
        <client>
            <endpoint address="http://myserver/Service/MyService.svc"
                binding="customBinding" bindingConfiguration="CustomBinding_IMyService"
                contract="MyService.IMyService" name="CustomBinding_IMyService" />
        </client>
    </system.serviceModel>
</configuration>

When I call the service using this config, it works, so the service is fine. But in my actual application I can't use the app.config, and need to build the service client in code. I tried to do a straightforward translation:

var binaryMessageEncoding = new BinaryMessageEncodingBindingElement();
var httpTransport = new HttpsTransportBindingElement()
                        {
                            Realm = "", ManualAddressing = false, 
                            HostNameComparisonMode = HostNameComparisonMode.StrongWildcard, 
                            TransferMode = TransferMode.Buffered, 
                            MaxBufferSize = int.MaxValue, 
                            MaxReceivedMessageSize = int.MaxValue, AllowCookies = false, 
                            AuthenticationScheme = AuthenticationSchemes.Anonymous, 
                            UnsafeConnectionNtlmAuthentication = false
                        };
var customBinding = new CustomBinding(binaryMessageEncoding, httpTransport);
var myServiceClient = new MyServiceClient(customBinding,
                                                    new EndpointAddress(
                                                        "http://myserver/Service/MyService.svc"));

But when I call the service, I get ArgumentException that says "The provided URI scheme 'http' is invalid; expected 'https'.", so the binding I built for some reason thinks it has to be secure... So, now we are back to the original question -- how do I disable security on a CustomBinding in code? I know that in config it's a simple <security mode="none"/>, but I can't use the config, and for some reason was not able to find the equivalent API.

+1  A: 

You ar using HttpsTransportBindingElement you should use HttpTransportBindingElement instead

CriGoT
+1  A: 

From what I see, you are instantiating a HttpsTransportBindingElement object. You might want to use a HttpTransportBindingElement object instead.

The HttpsTransportBindingElement object implicitly requires SSL security which is why you are getting an ArgumentException that says "The provided URI scheme 'http' is invalid; expected 'https'."

Patrick McElreavy
Thank you! (How could I miss it myself?... Jeez.)