2010-07-13 79 views
0

我有一個需要調用的WCF服務。當我通過VS「添加服務引用」,我獲得以下客戶端配置:如何通過代碼禁用WCF的CustomBinding的安全性?

<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> 

當我使用這個配置調用服務,它的工作原理,所以這項服務是好的。但在我的實際應用程序中,我無法使用app.config,並需要使用代碼構建服務客戶端。我試圖做一個簡單的翻譯:

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")); 

但是,當我撥打服務,我得到ArgumentException,說"The provided URI scheme 'http' is invalid; expected 'https'.",所以我建立某種原因認爲它是安全的結合。所以,現在我們回到原來的問題 - 我如何禁用代碼中的CustomBinding安全?我知道在配置中它是一個簡單的<security mode="none"/>,但我不能使用該配置,並出於某種原因無法找到相應的API。

回答

2

從我所看到的,你實例化一個HttpsTransportBindingElement對象。您可能想改爲使用HttpTransportBindingElement對象。

HttpsTransportBindingElement對象隱含要求SSL安全性這就是爲什麼你得到一個ArgumentException,說"The provided URI scheme 'http' is invalid; expected 'https'."

+0

謝謝! (我怎麼能錯過它自己?...... Jeez。) – user8032 2010-07-13 14:41:50

1

您使用AR HttpsTransportBindingElement你應該使用HttpTransportBindingElement代替