2012-04-17 92 views
1

我正在使用以編程方式配置的WCF客戶端(System.ServiceModel.ClientBase)。此WCF客戶端使用CustomBinding進行配置,默認情況下它具有TextMessageEncodingBindingElement。更改WCF System.ServiceModel.ClientBase的Endpoint.Binding不起作用

現在,當我嘗試切換到Mtom編碼時,我更改了客戶端的Endpoint.Binding屬性,該屬性工作正常。 Endpoint.Binding屬性顯示已更改。

不幸的是,當我執行WCF服務公開的方法之一時,它仍然使用TextMessageEncoding,我找不到原因。

我知道了,雖然工作,通過構建新的ClientBase和傳遞新的EndPointBinding在構造函數中:

socialProxy = new SocialProxyClient(SocialProxyClientSettings.SocialProxyMTomEndPointBinding, new EndpointAddress(SocialProxyClientSettings.SocialProxyEndPointAddress)); 

但是當我嘗試這樣它不工作:

socialProxy.Endpoint.Binding = SocialProxyClientSettings.SocialProxyMTomEndPointBinding; 

這些是我對EndPointBindings的定義:

public static TextMessageEncodingBindingElement TextMessageEncodingBindingElement 
{ 
    get 
    { 
     if (_textMessageEncodingBindingElement == null) 
     { 
      _textMessageEncodingBindingElement = new TextMessageEncodingBindingElement() { MessageVersion = MessageVersion.Soap11 }; 
      _textMessageEncodingBindingElement.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas() 
      { 
       MaxDepth = 32, 
       MaxStringContentLength = 5242880, 
       MaxArrayLength = 204800000, 
       MaxBytesPerRead = 5242880, 
       MaxNameTableCharCount = 5242880 
      }; 
     } 
     return _textMessageEncodingBindingElement; 
    } 
} 

public static MtomMessageEncodingBindingElement MtomMessageEncodingBindingElement 
{ 
    get 
    { 
     if (_mtomMessageEncodingBindingElement == null) 
     { 
      _mtomMessageEncodingBindingElement = new MtomMessageEncodingBindingElement(); 
      _mtomMessageEncodingBindingElement.MaxReadPoolSize = TextMessageEncodingBindingElement.MaxReadPoolSize; 
      _mtomMessageEncodingBindingElement.MaxWritePoolSize = TextMessageEncodingBindingElement.MaxWritePoolSize; 
      _mtomMessageEncodingBindingElement.MessageVersion = TextMessageEncodingBindingElement.MessageVersion; 
      _mtomMessageEncodingBindingElement.ReaderQuotas.MaxDepth = TextMessageEncodingBindingElement.ReaderQuotas.MaxDepth; 
      _mtomMessageEncodingBindingElement.ReaderQuotas.MaxStringContentLength = TextMessageEncodingBindingElement.ReaderQuotas.MaxStringContentLength; 
      _mtomMessageEncodingBindingElement.ReaderQuotas.MaxArrayLength = TextMessageEncodingBindingElement.ReaderQuotas.MaxArrayLength; 
      _mtomMessageEncodingBindingElement.ReaderQuotas.MaxBytesPerRead = TextMessageEncodingBindingElement.ReaderQuotas.MaxBytesPerRead; 
      _mtomMessageEncodingBindingElement.ReaderQuotas.MaxNameTableCharCount = TextMessageEncodingBindingElement.ReaderQuotas.MaxNameTableCharCount; 
     } 
     return _mtomMessageEncodingBindingElement; 
    } 
} 

有人可以解釋爲什麼chang Endpoint.Binding以編程方式不起作用?

回答

2

我相信在構建ClientBase的時候,原始的Binding被用來創建一些幫助對象。稍後更改綁定不會更改這些幫助程序對象。

要在構建之後做出任何調整,您可能需要自定義綁定行爲,您可以根據需要調整綁定的內部。在構造中使用它,以便爲以後的更改準備好所有幫助程序對象。像往常一樣,所有你想要的只是一個簡單的行爲改變,但你還需要編寫輔助助手類來支持你的行爲改變。

查看SO線程:ONVIF Authentication in .NET 4.0 with Visual Studio 2010 有關CustomBinding問題的討論。

查看博客文章:Supporting the WS-I Basic Profile Password Digest in a WCF Client Proxy 有關自定義行爲的示例,可讓您即時更改用戶名令牌。

也許可以做一些類似的事情,讓您在運行中控制本地端點綁定。

更新:更多閱讀在StackOverflow和它的頁面鏈接,我相信我找到了你正在尋找的答案。

對於PasswordDigestBehavior: 看到:ONVIF Authentication in .NET 4.0 with Visual Studios 2010 和:http://benpowell.org/supporting-the-ws-i-basic-profile-password-digest-in-a-wcf-client-proxy/

對於本地網卡綁定: 見:Specify the outgoing IP address to use with WCF client

// ASSUMPTIONS: 
// 1: DeviceClient is generated by svcutil from your WSDL. 
// 1.1: DeviceClient is derived from 
//   System.ServiceModel.ClientBase<Your.Wsdl.Device> 
// 2: serviceAddress is the Uri provided for your service. 
// 
private static DeviceClient CreateDeviceClient(IPAddress nicAddress, 
               Uri serviceAddress, 
               String username, 
               String password) 
{ 
    if (null == serviceAddress) 
     throw new ArgumentNullException("serviceAddress"); 

    ////////////////////////////////////////////////////////////////////////////// 
    // I didn't know how to put a variable set of credentials into a static 
    // app.config file. 
    // But I found this article that talks about how to set up the right kind 
    // of binding on the fly. 
    // I also found the implementation of PasswordDigestBehavior to get it all to work. 
    // 
    // from: https://stackoverflow.com/questions/5638247/onvif-authentication-in-net-4-0-with-visual-studios-2010 
    // see: http://benpowell.org/supporting-the-ws-i-basic-profile-password-digest-in-a-wcf-client-proxy/ 
    // 
    EndpointAddress serviceEndpointAddress = new EndpointAddress(serviceAddress); 
    HttpTransportBindingElement httpBinding = new HttpTransportBindingElement(); 
    if (!String.IsNullOrEmpty(username)) 
    { 
     httpBinding.AuthenticationScheme = AuthenticationSchemes.Digest; 
    } 
    else 
    { 
     httpBinding.AuthenticationScheme = AuthenticationSchemes.Anonymous; 
    } 
    var messageElement = new TextMessageEncodingBindingElement(); 
    messageElement.MessageVersion = 
     MessageVersion.CreateVersion(EnvelopeVersion.Soap12, AddressingVersion.None); 

    CustomBinding bind = new CustomBinding(messageElement, httpBinding); 

    //////////////////////////////////////////////////////////////////////////////// 
    // from: https://stackoverflow.com/questions/3249846/specify-the-outgoing-ip-address-to-use-with-wcf-client 
    // Adjust the serviceEndpointAddress to bind to the local NIC, if at all possible. 
    // 
    ServicePoint sPoint = ServicePointManager.FindServicePoint(serviceAddress); 
    sPoint.BindIPEndPointDelegate = delegate(
      System.Net.ServicePoint servicePoint, 
      System.Net.IPEndPoint remoteEndPoint, 
      int retryCount) 
    { 
     // if we know our NIC local address, use it 
     // 
     if ((null != nicAddress) 
      && (nicAddress.AddressFamily == remoteEndPoint.AddressFamily)) 
     { 
      return new System.Net.IPEndPoint(nicAddress, 0); 
     } 
     else if (System.Net.Sockets.AddressFamily.InterNetworkV6 == remoteEndPoint.AddressFamily) 
     { 
      return new System.Net.IPEndPoint(System.Net.IPAddress.IPv6Any, 0); 
     } 
     else // if (System.Net.Sockets.AddressFamily.InterNetwork == remoteEndPoint.AddressFamily) 
     { 
      return new System.Net.IPEndPoint(System.Net.IPAddress.Any, 0); 
     } 
    }; 
    ///////////////////////////////////////////////////////////////////////////// 

    DeviceClient client = new DeviceClient(bind, serviceEndpointAddress); 

    // Add our custom behavior 
    // - this requires the Microsoft WSE 3.0 SDK file: Microsoft.Web.Services3.dll 
    // 
    PasswordDigestBehavior behavior = new PasswordDigestBehavior(username, password); 
    client.Endpoint.Behaviors.Add(behavior); 

    return client; 
} 
+0

@Junto - 謝謝。 – 2014-12-16 16:29:37