从BasicHttpBindingElement创建BasicHttpBinding

时间:2014-06-10 20:05:16

标签: c# wcf wcf-binding

是否有一种简单的方法可以从BasicHttpBindingElement创建BasicHttpBinding,除了遍历所有属性并设置值?

这就是我现在正在做的事情

public class BasicHttpBinding : System.ServiceModel.BasicHttpBinding
{
    public BasicHttpBinding(BasicHttpBindingElement element)
    {
        this.AllowCookies = element.AllowCookies;
        this.BypassProxyOnLocal = element.BypassProxyOnLocal;
        this.CloseTimeout = element.CloseTimeout;
        this.HostNameComparisonMode = element.HostNameComparisonMode;
        this.MaxBufferPoolSize = element.MaxBufferPoolSize;
        this.MaxBufferSize = element.MaxBufferSize;
        this.MaxReceivedMessageSize = element.MaxReceivedMessageSize;
        this.Name = element.Name;
        this.OpenTimeout = element.OpenTimeout;
        this.ProxyAddress = element.ProxyAddress;
        this.ReceiveTimeout = element.ReceiveTimeout;
        this.Security.Message.AlgorithmSuite = element.Security.Message.AlgorithmSuite;
        this.Security.Message.ClientCredentialType = element.Security.Message.ClientCredentialType;
        this.Security.Mode = element.Security.Mode;
        this.SendTimeout = element.SendTimeout;
        this.TextEncoding = element.TextEncoding;
        this.TransferMode = element.TransferMode;
        this.UseDefaultWebProxy = element.UseDefaultWebProxy;
    }
}

2 个答案:

答案 0 :(得分:1)

var bindingConfig = ConfigurationManager.GetSection("system.serviceModel/bindings") as System.ServiceModel.Configuration.BindingsSection;
        var element = bindingConfig.BasicHttpBinding.ConfiguredBindings[2]; //Whatever index the binding you want is.
        var myBinding = (System.ServiceModel.Channels.Binding)Activator.CreateInstance(bindingConfig.BasicHttpBinding.BindingType);
        element.ApplyConfiguration(myBinding);//This is what adds the configuration to the binding.

以下是我发现的景象:http://weblogs.asp.net/cibrax/getting-wcf-bindings-and-behaviors-from-any-config-source

答案 1 :(得分:0)

使用带有名称的重载并在配置文件中将其命名。这样您就不必手动访问该元素。