开发者

How do I disable security on a WCF's CustomBinding via code?

开发者 https://www.devze.com 2023-01-07 04:06 出处:网络
I have a WCF service that I need to call. When I go through \"add service reference\" in VS, I get following client config:

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="fal开发者_开发知识库se" 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.


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'."


You ar using HttpsTransportBindingElement you should use HttpTransportBindingElement instead

0

精彩评论

暂无评论...
验证码 换一张
取 消