【问题标题】:How can I programmatically create this custom binding?如何以编程方式创建此自定义绑定?
【发布时间】:2011-02-02 22:12:54
【问题描述】:

我们必须访问一个使用soap11的网络服务...没问题我将绑定设置为:

BasicHttpBinding wsBinding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential);

不。没有骰子。所以我问服务的主机为什么我们会遇到身份验证问题,他说我们的配置需要有这个自定义绑定:

<bindings>
    <customBinding>
        <binding name="lbinding">
            <security  authenticationMode="UserNameOverTransport" 
                messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11" 
                securityHeaderLayout="Strict" 
                includeTimestamp="false"
                requireDerivedKeys="true" 
                keyEntropyMode="ServerEntropy">
            </security>
            <textMessageEncoding messageVersion="Soap11" />
            <httpsTransport authenticationScheme ="Negotiate" requireClientCertificate ="false" realm =""/>
        </binding>
    </customBinding>
</bindings>

唯一的问题是我们以编程方式创建绑定,而不是通过配置。因此,如果有人能指出我正确的方向,将我的 BasicHttpBinding 更改为符合 .config 值的自定义绑定,前提是我会在当天给他们一颗闪亮的金星。

【问题讨论】:

    标签: wcf wcf-binding basichttpbinding


    【解决方案1】:

    解决了!

    对于那些处于类似困境的人来说,这是获胜的代码。

    Uri epUri = new Uri(_serviceUri);
    CustomBinding binding = new CustomBinding();
    SecurityBindingElement sbe = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
    sbe.MessageSecurityVersion = MessageSecurityVersion.WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11;        
    sbe.SecurityHeaderLayout = SecurityHeaderLayout.Strict;
    sbe.IncludeTimestamp = false;
    sbe.SetKeyDerivation(true);
    sbe.KeyEntropyMode = System.ServiceModel.Security.SecurityKeyEntropyMode.ServerEntropy;
    binding.Elements.Add(sbe);
    binding.Elements.Add(new TextMessageEncodingBindingElement(MessageVersion.Soap11, System.Text.Encoding.UTF8));
    binding.Elements.Add(new HttpsTransportBindingElement());
    EndpointAddress endPoint = new EndpointAddress(epUri);
    

    【讨论】:

    • 谢谢您。我有一个类似的问题,我必须创建另一个包含执行外部服务调用逻辑的类项目。我真的没有(不能)将配置从 app.config 移动到底层的 web.config - 因此上述解决方案非常适合这种情况,可以覆盖服务定义。
    • 谢谢 - 有一个 C# dll 供 Coldfusion 使用,所以...没有 app.config :( 您的示例帮助我将 TextMessage 编码和 https 传输添加到自定义绑定 - 非常感谢!
    • 如何向此自定义绑定添加基本身份验证?
    【解决方案2】:

    @D。 Forrest 已经找到了解决方案,但是查看给定 WCF 配置涉及哪些对象的简单方法是在您正在使用的客户端代理上调用 .Endpoint.Binding.CreateBindingElements()。您可以转储返回的列表中每个项目的对象树,并查看绑定是如何配置的。

    【讨论】:

    • 非常感谢您提及此方法!我使用它在运行时将现有的NetTcpBinding 转换为自定义绑定,以启用 GZIP 压缩。这样我就不需要弄清楚如何手动进行转换,也不需要硬编码所需的绑定元素。
    • 事实证明它更容易——你可以简单地将任何现有的Binding 传递给CustomBinding 构造函数,根本不需要那个方法。还记录了here。对于熟悉 WCF 的人来说可能很明显,但也许这有助于像我这样的其他一些困惑的人:)
    【解决方案3】:

    您可以使用:

            Uri epUri = new Uri("http://localhost/TestWCFService/Service.svc");
            CustomBinding binding = new CustomBinding()
            {
                Name = "anyname",
                ReceiveTimeout = new TimeSpan(0, 0, 10, 0, 0),
                SendTimeout = new TimeSpan(0, 0, 10, 0, 0),
            };
            var element1 = new TextMessageEncodingBindingElement()
            {
                ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
                {
                    MaxDepth = 2147483647,
                    MaxStringContentLength = 2147483647,
                    MaxArrayLength = 2147483647,
                    MaxBytesPerRead = 2147483647,
                    MaxNameTableCharCount = 2147483647
                }
            };
            var element2 = new HttpsTransportBindingElement()
            {
                ManualAddressing = false,
                MaxReceivedMessageSize = 2147483647,
                AllowCookies = false,
                AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous,
                BypassProxyOnLocal = false,
                MaxBufferSize = 2147483647,
                ProxyAuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous,
                TransferMode = TransferMode.Buffered,
                UseDefaultWebProxy = true
            };
            var element3 = new TextMessageEncodingBindingElement(MessageVersion.Soap11, System.Text.Encoding.UTF8);
    
            binding.Elements.Add(element1);
            binding.Elements.Add(element2);
            binding.Elements.Add(element3);
    
            //binding.Elements.Add(new HttpsTransportBindingElement());
    
            EndpointAddress endPoint = new EndpointAddress(epUri);
            var client = new ServiceClient(binding, endPoint);
    

    【讨论】:

      猜你喜欢
      • 2012-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多