如何以编程方式创建此自定义绑定?

时间:2010-03-31 12:56:01

标签: wcf wcf-binding basichttpbinding

我们必须访问使用soap11的Web服务...没问题我只需将绑定设置为:

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值的自定义绑定,那么我会给他们一个闪亮的金色星星。

3 个答案:

答案 0 :(得分:32)

解决了!

这是处于类似困境的人的获胜代码。

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

答案 1 :(得分:7)

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

答案 2 :(得分:2)

您可以使用:

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