WCF - how to create programatically custom binding with binary encoding over HTTP(S)

后端 未结 3 802
有刺的猬
有刺的猬 2021-02-09 19:53

I\'d like to convert my current HTTP/HTTPS WCF binding settings to use binary message encoding and I need to do it in code - not in XML configuration. AFAIK it\'s necessary to

相关标签:
3条回答
  • 2021-02-09 20:32

    I almost forgot this question, but here is my custom binding class which works with binary binding over HTTP with username+password validation and also allows to turn GZip compression on...

        public class CustomHttpBinding: CustomBinding
    {
        private readonly bool useHttps;
        private readonly bool useBinaryEncoding;
        private readonly bool useCompression;
        private readonly HttpTransportBindingElement transport;
    
        public CustomHttpBinding(bool useHttps, bool binaryEncoding = true, bool compressMessages = false)
        {
            this.useHttps = useHttps;
            transport = useHttps ? new HttpsTransportBindingElement() : new HttpTransportBindingElement();
            useBinaryEncoding = binaryEncoding;
            useCompression = compressMessages;
        }
    
        public long MaxMessageSize{set
        {
            transport.MaxReceivedMessageSize = value;
            transport.MaxBufferSize = (int) value;
        }}
    
        public override BindingElementCollection CreateBindingElements()
        {
            BindingElement security;
            if (useHttps)
            {
                security = SecurityBindingElement.CreateSecureConversationBindingElement(
                    SecurityBindingElement.CreateUserNameOverTransportBindingElement());
            }
            else
            {
                security = SecurityBindingElement.CreateSecureConversationBindingElement(
                    SecurityBindingElement.CreateUserNameForSslBindingElement(true));
            }
    
            MessageEncodingBindingElement encoding;
            if (useCompression)
            {
                encoding = new GZipMessageEncodingBindingElement(useBinaryEncoding
                                                                    ? (MessageEncodingBindingElement)
                                                                      new BinaryMessageEncodingBindingElement()
                                                                    : new TextMessageEncodingBindingElement());
            }
            else
            {
                encoding = useBinaryEncoding
                            ? (MessageEncodingBindingElement) new BinaryMessageEncodingBindingElement()
                            : new TextMessageEncodingBindingElement();
            }
    
            return new BindingElementCollection(new[]
                {
                    security,
                    encoding,
                    transport,
                });
        }
    }
    
    0 讨论(0)
  • 2021-02-09 20:42

    The SecurityBindingElement has a AllowInsecureTransport property. If you set this to true you can use the HttpTransportBindingElement with message user name and password security.

    0 讨论(0)
  • Try SecurityBindingElement.CreateUserNameOverTransportBindingElement() instead:

    var custBinding = new CustomBinding();
    custBinding.Elements.Add(new BinaryMessageEncodingBindingElement());
    //Transport Security (Not Required)
    if (isHttps)
    {
      custBinding.Elements.Add(SecurityBindingElement.CreateUserNameOverTransportBindingElement());
    }
    //Transport (Required)
    custBinding.Elements.Add(isHttps ?
       new HttpsTransportBindingElement() :
       new HttpTransportBindingElement());
    
    0 讨论(0)
提交回复
热议问题