程序代码中的WCF客户端绑定配置

时间:2011-01-13 22:36:45

标签: wcf

我有以下类来配置安全性,编码和令牌参数,但是我在添加BasicHttpBinding以指定MaxReceivedMessageSize时遇到问题。任何见解都将不胜感激。

    public class MultiAuthenticationFactorBinding
{
    public static Binding CreateMultiFactorAuthenticationBinding()
    {
        HttpsTransportBindingElement httpTransport = new HttpsTransportBindingElement();
        CustomBinding binding = new CustomBinding();
        binding.Name = "myCustomBinding";

        TransportSecurityBindingElement messageSecurity = TransportSecurityBindingElement.CreateUserNameOverTransportBindingElement();
        messageSecurity.AllowInsecureTransport = true;
        messageSecurity.EnableUnsecuredResponse = true;
        messageSecurity.MessageSecurityVersion = MessageSecurityVersion.WSSecurity11WSTrust13WSSecureConversation13WSSecurityPolicy12;
        messageSecurity.SecurityHeaderLayout = SecurityHeaderLayout.Strict;
        messageSecurity.IncludeTimestamp = true;
        messageSecurity.SetKeyDerivation(false);

        TextMessageEncodingBindingElement Quota = new TextMessageEncodingBindingElement(MessageVersion.Soap11, System.Text.Encoding.UTF8);
        Quota.ReaderQuotas.MaxDepth = 32;
        Quota.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
        Quota.ReaderQuotas.MaxArrayLength = 16384;
        Quota.ReaderQuotas.MaxBytesPerRead = 4096;
        Quota.ReaderQuotas.MaxNameTableCharCount = 16384;


        X509SecurityTokenParameters clientX509SupportingTokenParameters = new X509SecurityTokenParameters();
        clientX509SupportingTokenParameters.InclusionMode = SecurityTokenInclusionMode.AlwaysToRecipient;
        clientX509SupportingTokenParameters.RequireDerivedKeys = false;
        messageSecurity.EndpointSupportingTokenParameters.Endorsing.Add(clientX509SupportingTokenParameters);
        //binding.ReceiveTimeout = new TimeSpan(0,0,300);
        binding.Elements.Add(Quota);
        binding.Elements.Add(messageSecurity);
        binding.Elements.Add(httpTransport);
        return binding;
    }
}

2 个答案:

答案 0 :(得分:1)

如果您需要指定MaxReceivedMessageSize,则可以在传输绑定元素HttpsTransportBindingElement上执行此操作。您无法将绑定添加到绑定。

答案 1 :(得分:0)

Just Found this创建一个BasicHttpBinding

BasicHttpBinding Basicbinding = new  BasicHttpBinding(BasicHttpSecurityMode.None);
Basicbinding.MaxReceivedMessageSize = 10000000;