1. Create a custom binding
Sometimes we need to create our own binding, which is not due to special security requirements and transmission protocols used in many cases. To create a custom binding, you need to create a group of binding elements. The binding element is derived from System. ServiceModel. Channels. BindingElement.
Code 1: custom binding using HTTP transmission protocol and Binary encoding format:
<bindings> <customBinding> <binding name="binHttp"> <binaryMessageEncoding />
In addition to creating a custom binding by configuration, you can also use programming.
Code 2: configure a custom binding programmatically:
class Program { static void Main(string[] args) { CustomBinding binding = new CustomBinding(); binding.Elements.Add(new BinaryMessageEncodingBindingElement()); binding.Elements.Add(new HttpTransportBindingElement()); ServiceHost host = new ServiceHost(typeof(CarRentalService)); ServiceEndpoint serviceEndpoint = host.AddServiceEndpoint(typeof(ICarRentalService), binding, "http://localhost:8080/CarRentalService"); try { host.Open(); Console.WriteLine("The car rental service is up and listening on the following addresses:"); foreach (var item in host.Description.Endpoints) { Console.WriteLine("> [{0}] {1}", item.Binding.Name, item.Address.Uri.ToString()); } Console.ReadLine(); host.Close(); } catch (CommunicationException ex) { host.Abort(); Console.WriteLine(ex); } catch (TimeoutException ex) { host.Abort(); Console.WriteLine(ex); } catch (Exception ex) { host.Abort(); Console.WriteLine(ex); } } }
2. Reuse custom binding
Sometimes, you may need to create a custom binding that can be repeated for different solutions. Since each Binding is derived from the abstract base class System. ServiceModel. Channels. Binding, we only need to create our own Binding class and implement the CreateBindingElement method.
Code 1: Creates a reusable custom Binding by the base class Binding:
public class NetTcpTextBinding : Binding { private TcpTransportBindingElement transport; private TextMessageEncodingBindingElement encoding; public NetTcpTextBinding() : base() { this.Initialize(); } public override BindingElementCollection CreateBindingElements() { BindingElementCollection elements = new BindingElementCollection(); elements.Add(this.encoding); elements.Add(this.transport); return elements; } public override string Scheme { get { return this.transport.Scheme; } } private void Initialize() { this.transport = new TcpTransportBindingElement(); this.encoding = new TextMessageEncodingBindingElement(); } }
Code 2: bind with NetTcpTextBing:
NetTcpTextBinding binding = new NetTcpTextBinding(); ServiceHost host = new ServiceHost(typeof(Service1)); host.AddServiceEndpoint(typeof(IService1), binding, "net.tcp://localhost:10101/IService"); host.Open();
To use custom binding through the configuration file, you must first extend the BindingCollectionElement abstract base class and then implement the required method.
Code 3: Implement NetTcpTextBindingCollectionElement:
namespace ConsoleApplication1{ public class NetTcpTextBindingCollectionElement : BindingCollectionElement { public override Type BindingType { get { return typeof(NetTcpTextBinding); } } public override ReadOnlyCollection<IBindingConfigurationElement>ConfiguredBindings { get { return new ReadOnlyCollection<IBindingConfigurationElement>(new List<IBindingConfigurationElement>()); } } public override bool ContainsKey(string name) { throw new NotImplementedException(); } protected override Binding GetDefault() { return new NetTcpTextBinding(); } protected override bool TryAdd(string name, Binding binding, Configuration config) { throw new NotImplementedException(); } }}
Now, to use custom binding NetTcpTextBinding in the configuration file, you must use the binding extension (<bindingExtensions>) to configure the NetTcpTextBindingCollectionElement. To use custom binding in the configuration file, you must first define the binding.
Code 4: Use custom binding NetTcpTextBinding in the configuration file:
<system.serviceModel> <services> <service name="WcfServiceLibrary2.Service1"> <endpoint address="net.tcp://localhost:10101/IService" binding="netTcpTextBinding" contract="WcfServiceLibrary2.IService1"> </endpoint> </service> </services> <extensions> <bindingExtensions> <add name="netTcpTextBinding" type="ConsoleApplication1.NetTcpTextBindingCollectionElement, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> </bindingExtensions> </extensions> </system.serviceModel>