How can I programmatically create this custom binding?

僤鯓⒐⒋嵵緔 提交于 2019-12-02 16:21:05

Solved it!

Here's the winning code for those who are in a similar predicament.

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

@D. Forrest already found the solution, but a simple way to see what objects are involved for a given WCF configuration is to call .Endpoint.Binding.CreateBindingElements() on the client proxy you are using. You can the dump the object tree of each item in the list that is returned and see how the binding is configured.

You can use :

        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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!