WCF CustomBinding in codes

It’s very likely that you configure your WCF bindings in the configuration file and it absolutely make sense if you have to adjust the settings more often. Would not it be good to create such bindings in code if they don’t change and you control both the service and the client?

In the following section we will create two CustomBinding- one with BinaryMessageEncoding (for .net clients) and another for TextMessageEncoding (for inter-operable clients). All the property settings are shown here for illustration purpose assuming the service is hosted in IIS and you must change them as per your requirements.

CustomBindingBinary:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;
using System.Configuration;
using System.ServiceModel;

namespace WCF.Bindings
{
	public class CustomBindingBinary :CustomBinding
	{
		private BindingElementCollection elements = null;

		public CustomBindingBinary()
		{
			Init();
		}

		private void Init()
		{
			elements = base.CreateBindingElements();

			this.CloseTimeout = TimeSpan.FromMinutes(2);
			this.ReceiveTimeout = TimeSpan.FromMinutes(2);
			this.SendTimeout = TimeSpan.FromMinutes(2);
			this.OpenTimeout = TimeSpan.FromMinutes(2);

			BinaryMessageEncodingBindingElement binaryEncoding = new BinaryMessageEncodingBindingElement();
			binaryEncoding.MaxReadPoolSize = 128;
			binaryEncoding.MaxWritePoolSize = 50;
			binaryEncoding.ReaderQuotas.MaxArrayLength = 2147483647;
			binaryEncoding.ReaderQuotas.MaxStringContentLength = 2147483647;
			binaryEncoding.ReaderQuotas.MaxDepth = 32;
			binaryEncoding.ReaderQuotas.MaxBytesPerRead = 2147483647;
			binaryEncoding.ReaderQuotas.MaxNameTableCharCount = 2147483647;

			elements.Remove<BinaryMessageEncodingBindingElement>();
			elements.Add(binaryEncoding);
				
			HttpTransportBindingElement transport = new HttpTransportBindingElement();
			transport.AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous; //Adjust your security setting as per requirements.
			transport.MaxBufferPoolSize = 2147483647;
			transport.MaxBufferSize = 2147483647;
			transport.MaxReceivedMessageSize = 2147483647;
			transport.TransferMode = TransferMode.Buffered;

			elements.Remove<HttpTransportBindingElement>();
			elements.Add(transport);	
			
			base.Elements.AddRange(this.elements.ToArray<BindingElement>());
			
		}
	}
}

CustomBindingText:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;
using System.Configuration;
using System.ServiceModel;

namespace WCF.Bindings
{
	public class CustomBindingText : CustomBinding
	{
		private BindingElementCollection elements = null;

		public CustomBindingText()
		{
			Init();
		}

		private void Init()
		{
			elements = base.CreateBindingElements();

			this.CloseTimeout = TimeSpan.FromMinutes(2);
			this.ReceiveTimeout = TimeSpan.FromMinutes(2);
			this.SendTimeout = TimeSpan.FromMinutes(2);
			this.OpenTimeout = TimeSpan.FromMinutes(2);

			TextMessageEncodingBindingElement textEncoding = new TextMessageEncodingBindingElement();
			textEncoding.MessageVersion = MessageVersion.Soap11;
			textEncoding.WriteEncoding = Encoding.UTF8;
			textEncoding.MaxReadPoolSize = 128;
			textEncoding.MaxWritePoolSize = 50;
			textEncoding.ReaderQuotas.MaxArrayLength = 2147483647;
			textEncoding.ReaderQuotas.MaxStringContentLength = 2147483647;
			textEncoding.ReaderQuotas.MaxDepth = 32;
			textEncoding.ReaderQuotas.MaxBytesPerRead = 2147483647;
			textEncoding.ReaderQuotas.MaxNameTableCharCount = 2147483647;
						
			elements.Remove<TextMessageEncodingBindingElement>();
			elements.Add(textEncoding);

			HttpTransportBindingElement transport = new HttpTransportBindingElement();
			transport.AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous; //Adjust your security setting as per requirements.
			transport.MaxBufferPoolSize = 2147483647;
			transport.MaxBufferSize = 2147483647;
			transport.MaxReceivedMessageSize = 2147483647;
			transport.TransferMode = TransferMode.Buffered;

			elements.Remove<HttpTransportBindingElement>();
			elements.Add(transport);

			base.Elements.AddRange(this.elements.ToArray<BindingElement>());
		}

	}
}

Leave a Reply