How can I programmatically create this custom binding?

后端 未结 3 1018
情深已故
情深已故 2020-12-23 15:21

We\'ve got to access a web service that uses soap11... no problem I\'ll just set the binding as:

BasicHttpBinding wsBinding = new BasicHttpBinding(BasicHttpS         


        
相关标签:
3条回答
  • 2020-12-23 15:28

    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);
    
    0 讨论(0)
  • 2020-12-23 15:35

    @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.

    0 讨论(0)
  • 2020-12-23 15:49

    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);
    
    0 讨论(0)
提交回复
热议问题