从BasicHttpBindingElement创建一个BasicHttpBinding

本文关键字:一个 BasicHttpBinding BasicHttpBindingElement 创建 | 更新日期: 2023-09-27 18:08:15

是否有一个简单的方法来创建一个BasicHttpBinding从BasicHttpBindingElement除了循环通过所有属性和设置值?

这就是我现在正在做的

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

从BasicHttpBindingElement创建一个BasicHttpBinding

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

使用接受名称并在配置文件中命名的重载。这样你就不需要手动访问元素了。

相关文章: