Machinegon
Machinegon

Reputation: 1885

Create a BasicHttpBinding from a BasicHttpBindingElement

Is there an easy way to create a BasicHttpBinding from a BasicHttpBindingElement except looping through all properties and set the values?

This is what I'm doing right now

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

Upvotes: 0

Views: 1378

Answers (2)

Beggs
Beggs

Reputation: 46

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.

Here is the sight I found it on: http://weblogs.asp.net/cibrax/getting-wcf-bindings-and-behaviors-from-any-config-source

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 190945

Use the overload that takes a name and name it in your config file. That way you won't have to manually access the element.

Upvotes: 0

Related Questions