Wednesday, February 22, 2012

WCF : Creating a binding(basichttp/nettcp) based on configuration

I am going to write a method here which will based on configuration return a BasicHttpBinding or NetTcpBinding. You will need to do something like this during development / qa phase when you wants to change you service infrastructure based on available resources.

In this piece of code, I am also  going to set ReaderQuotas to read max possible string, as in 1 of service we were returning a whole chunk of data.


public Binding GetBinding()
        {
            if (ConfigurationManager.Appsettig["ServiceKeyProtocol"].Equals("net.tcp"))
            {
                NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
                binding.MaxReceivedMessageSize = Int32.MaxValue;
                binding.ReaderQuotas.MaxArrayLength = Int32.MaxValue;
                binding.ReaderQuotas.MaxBytesPerRead = Int32.MaxValue;
                binding.ReaderQuotas.MaxDepth = Int32.MaxValue;
                binding.ReaderQuotas.MaxNameTableCharCount = Int32.MaxValue;
                binding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
                return binding;
            }
            else 
            {
                BasicHttpBinding httpbinding = new BasicHttpBinding(BasicHttpSecurityMode.None);
                httpbinding.MaxReceivedMessageSize = Int32.MaxValue;
                httpbinding.ReaderQuotas.MaxArrayLength = Int32.MaxValue;
                httpbinding.ReaderQuotas.MaxBytesPerRead = Int32.MaxValue;
                httpbinding.ReaderQuotas.MaxDepth = Int32.MaxValue;
                httpbinding.ReaderQuotas.MaxNameTableCharCount = Int32.MaxValue;
                httpbinding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
 
                return httpbinding;
            }
        }

No comments:

Post a Comment