2

我正在使用以编程方式配置的 WCF 客户端 (System.ServiceModel.ClientBase)。此 WCF 客户端使用 CustomBinding 进行配置,默认情况下它具有 TextMessageEncodingBindingElement。

现在,当我尝试切换到 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;
    }
}

有人可以解释为什么以编程方式更改 Endpoint.Binding 不起作用吗?

4

1 回答 1

3

我相信在ClientBase的构建过程中,原来的Binding是用来创建一些辅助对象的。稍后更改绑定不会更改这些辅助对象。

要在构建后进行任何调整,您可能需要自定义绑定行为,您可以根据需要调整绑定的内部。在构造中使用它,以便为您以后的更改准备所有帮助对象。像往常一样,您只需要一种简单的行为改变,但您还需要编写辅助帮助类来支持您的一种行为改变。

有关CustomBinding 问题的讨论,请参阅 SO 线程:使用 Visual Studio 2010 的 .NET 4.0 中的 ONVIF 身份验证。

请参阅博客文章:在 WCF 客户端代理中支持 WS-I 基本配置文件密码摘要 有关可让您动态更改用户名令牌的自定义行为的示例。

也许可以做类似的事情来让您动态控制本地端点绑定。

更新: 在 StackOverflow 中阅读更多内容,以及它链接到的页面,我相信我已经找到了您正在寻找的答案。

对于 PasswordDigestBehavior:请参阅:带有 Visual Studios 2010 的 .NET 4.0 中的 ONVIF 身份验证 和:http ://benpowell.org/supporting-the-ws-i-basic-profile-password-digest-in-a-wcf-client-proxy /

对于本地 NIC 绑定:请参阅:指定要与 WCF 客户端一起使用的传出 IP 地址

// 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;
}
于 2012-10-02T18:08:20.563 回答