Binding in WCF

Discussion in 'ASP.NET' started by MinalS, Nov 20, 2014.

  1. MinalS

    MinalS New Member

    Joined:
    Jul 8, 2014
    Messages:
    138
    Likes Received:
    32
    Trophy Points:
    0
    There are several bindings provided by the framework. We shall learn about these bindings in detail in this article. The following list consists of the bindings provided by the .NET Framework.
    1. basicHttpBinding: This binding is useful for the ASMX web services.
    2. wsHttpBinding: This binding is used for advanced WS – based web services. These services include the WS- security, WS – Transactions and many more.
    3. wsDualHttpBinding: This binding is used for bidirectional communication using the duplex contracts
    4. netTcpBinding: The binding for communication between the two .NET based systems.
    5. netNamedPipeBinding: The binding is used for communication between one or more machines
    6. netMsmqBinding: The binding is used for asynchronous communication using the Microsoft Message Queue.
    7. netPeerTcpBinding: The binding is used for creating peer – to – peer applications
    8. msmqIntegrationBinding: It is used for the message communication through the MSMQ queries
    9. wsFederationHttpBinding: The binding is used for WS- based web services
    The above binding can be used in either code or configuration. The below code demonstrates the basicHttpBinding in the configuration file.
    Code:
    <? xml version=”1.0” encoding=”utf-8” ?>
    <configuration>
    <system.serviceModel>
        <client>
        <endpoint address=”http://localhost/Welcome” binding=”basicHttpBinding” 
        contract=”IntoWCF.Welcome” >
        </endpoint>
        </client>
    </system.serviceModel>
    </configuration>
    
    The class containing the basicHttpBinding is as shown below:
    Code:
    using System;
    using System.Collection.Generic;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    
    namespace InfoWCF
    {
        class Program
        {
            static void Main( string[] args )
            {
                BasicHttpBinding binding=new BasicHttpBinding();
                using ( Welcome client=new Welcome ( Binding, “http://localhost/Welcome” ) ) client.Welcome(“user”);
            Console.ReadLine();
            }
        }
    }
    

    Selecting an Appropriate Binding



    Many Binding are provided by the WCF for communication between the client and the server. Every individual binding has a specific meaning. User can select the binding considering the security, reliability, manageability, interpretability, and performance.

    Every binding has communication details specified to the user. The factors specified in the details are cross machine, on – machine and interoperable communication through the Web services.

    The cross machine communication between the .NET applications



    In this section, user shall learn which bindings are used for the cross machine communication between the .NET applications.

    1) netTcpBinding

    The netTcpBinding is used for supporting the communication between the .NET applications deployed on the individual machines across the network. The communication over the internet and intranets is also supported. The user does not need any interoperability as they are developed on the .NET.

    The netTcpBinding is used for binary encoding. The TCP protocol is used for gaining the performance across the network. The following code is used to demonstrate the addressing syntax for the netTcpBinding.

    Code:
    net.tcp://{hostname}[ : port ] / { service location }
    
    Some of the properties of the netTcpBinding are as mentioned below:
    1. listenBacklog: It defines the maximum number of channels waiting for the service to request.
    2. closeTimeout: It states the maximum time to wait for the connection to close.
    3. maxBufferPoolSize: It is the maximum size of any buffer pools that are used for the transport
    4. name: It is the name of the binding.
    5. security: It defines the security settings of the bindings
    6. transactionFlow: It enables the transactions to flow from the client to the server.
    The configuration code for the netTcpBinding is as shown below:
    Code:
    <? xml version=”1.0” encoding=”utf-8” ?>
    <configuration>
    <system.serviceModel>
        <services>
         <service name=”InfoWCF.ProductSerivce” >
        <host>
        <baseAddresses>
            <add baseAddress=”net.tcp://localhost/productservice” />
        </baseAddresses>
        </host>
        <endpoint address=”” contract=”InfoWCF.IProductService” binding=”netTcpBinding” />
        </service>
        </services>
    </system.serviceModel>
    </configuration>
    
    The client configuration to use the service through the netTcpBinding is as shown below:
    Code:
    <?xml version=”1.0” encoding=”utf-8”?>
    <configuration>
        <system.serviceModel>
        <client>
            <endpoint address=”net.tcp://localhost/productservice” binding=”netTcpBinding” contract=”InfoWCF.IProductService” >
            </endpoint>
        </client>
        </system.serviceModel>
    </configuration>
    

    The local machine communication in the .NET applications



    When two processes are running on the same machine, it is known as intraprocess or crossprocess communication. The intraprocess communication refers to communication between two software components executing within the process. This process is known as local machine communication.

    Intra – appdomain or in – appdomain : The communication that occurs within a single .NET applications executing in a single application domain.

    Inter – appdomain or cross – appdomain: The communication that occurs between two .NET applications that are executing in different app domains in the same Windows process.

    The WCF service cannot discriminate in the interprocess, intraprocess or inter-appdomain communication. It provides a single on-machine transport channel based on named pipes. They are used as standard means for interprocess communication.

    netNamedPipeBinding

    It supports interprocess and intraprocess communication with netNamedPipeBinding. It is very useful in interprocess communication as it provides a great performance increase as compared to the other bindings in WCF.

    An address using the netNamedPipeBinding is as shown below:
    Code:
    net.pipe://localhost/{ service location }
    
    Some of the properties of the netNamesPipeBinding are as shown below:
    1. closeTimeout: It is used to specify the maximum time to wait for the connection to be closed.
    2. maxBufferPoolSize: It is the maximum size of any buffer pools to be used for the transport.
    3. maxConnections: It is the maximum number of connections that are inbound and outbound.
    4. name: It is the name of the binding
    5. receiveTimeout: It the maximum time to wait for the receive operation to complete
    The following configuration shows the netNamesPipeBinding in the file.
    Code:
     <? xml version=”1.0” encoding=”utf-8” ?>
    <configuration>
    <system.serviceModel>
        <services>
         <service name=”InfoWCF.ProductSerivce” >
        <host>
        <baseAddresses>
            <add baseAddress=”net.tcp://localhost/productservice” />
        </baseAddresses>
        </host>
        <endpoint address=”” contract=”InfoWCF.IProductService” binding=”netNamedPipeBinding” />
        </service>
        </services>
    </system.serviceModel>
    </configuration>
    
    The client configuration for the netNamedPipeBinding is as shown below:
    Code:
    <?xml version=”1.0” encoding=”utf-8”?>
    <configuration>
        <system.serviceModel>
        <client>
            <endpoint address=”net.tcp://localhost/productservice” binding=”netNamedPipeBinding” contract=”InfoWCF.IProductService” >
            </endpoint>
        </client>
        </system.serviceModel>
    </configuration>
    

    Communication using the Web Services



    The web services are useful when the communication is to be established in heterogeneous systems. Before the introduction of WCF, the ASP.NET Web Services (ASMX) and Web Service Enhancements (WSE) were used for the communication.

    In .NET 3.0, WCF was introduced. It provided user with single unified framework for building web services.

    basicHttpBinding

    The basicHttpBinding is used for supporting the Web Service communication. It provides support for interoperability across heterogeneous system. It does not support the latest web service standards.

    The following code shows the formats for basicHttpBinding is as shown below:
    Code:
    http://{hostname} [ : port ] / { service location }
    https://{hostname} [ : port ]/{ service location }
    
    Some of the properties for basicHttpBinding are as shown below:
    1. hostNameComparisonMode: It specifies the method for the hostname comparison when the URI is processed
    2. openTimeout: It is the maximum time to wait for the open connection to complete
    3. proxyAddress: It is used to specify the Web proxy to be used
    4. readerQuotas: It is used to specify the complexity of messages that can be processed
    5. receiveTimeout: It is the maximum time for waiting to receive operation to complete
    The following configuration information using basicHttpBinding is as shown below:
    Code:
    <? xml version=”1.0” encoding=”utf-8” ?>
    <configuration>
    <system.serviceModel>
        <services>
         <service name=”InfoWCF.ProductSerivce” >
        <host>
        <baseAddresses>
            <add baseAddress=”http://localhost/productservice” />
        </baseAddresses>
        </host>
        <endpoint address=”” contract=”InfoWCF.IProductService” binding=”basicHttpBinding” />
        </service>
        </services>
    </system.serviceModel>
    </configuration>
    
    The client configuration to use the service is as shown below:
    Code:
    <?xml version=”1.0” encoding=”utf-8”?>
    <configuration>
        <system.serviceModel>
        <client>
            <endpoint address=”http://localhost/productservice” binding=”basicHttpBinding” contract=”InfoWCF.IProductService” >
            </endpoint>
        </client>
        </system.serviceModel>
    </configuration>
    

    Communication through Advanced Services



    The advanced web services are the one that are exposed to the WS-* specifications. It includes security, reliable messaging, and transactions.

    wsHttpBinding

    It supports the WS-* which is added in the WCF framework. It provides advanced support to the infrastructure level protocols. It is the default binding for the .NET framework 3.0.

    The following code shows the addressing formats for the wsHttpBinding.

    Code:
    http://{hostname} : {port} / {service location}
    https://{hostname} : {port} / {service location}
    
    Some of the properties of the wsHttpBinding are as mentioned below:
    1. bypassProxyOnLocal: It bypasses the proxy settings for the local endpoints.
    2. hostNameComparisonMode: It specifies the method for hosting comparison between the parsing URI’s
    3. messageEncoding: This encoding type is used to encode messages
    4. textEncoding: It specifies how the messages are sent across the network.
    5. useDefaultWebProxy: It uses the default proxy specified by the operating system
    The configuration details for the wsHttpBinding are as shown below:
    Code:
     <? xml version=”1.0” encoding=”utf-8” ?>
    <configuration>
    <system.serviceModel>
        <services>
         <service name=”InfoWCF.ProductSerivce” >
        <host>
        <baseAddresses>
            <add baseAddress=”http://localhost/productservice” />
        </baseAddresses>
        </host>
        <endpoint address=”” contract=”InfoWCF.IProductService” binding=”wsHttpBinding” />
        </service>
        </services>
    </system.serviceModel>
    </configuration>
    
    The client configuration for the wsHttpBinding is as shown below:
    Code:
    <?xml version=”1.0” encoding=”utf-8”?>
    <configuration>
        <system.serviceModel>
        <client>
            <endpoint address=”http://localhost/productservice” binding=”wsHttpBinding” contract=”InfoWCF.IProductService” >
            </endpoint>
        </client>
        </system.serviceModel>
    </configuration>
    
    wsDualHttpBinding

    The wsDualHttpBinding is used for providing duplex communication. It consists of two binding elements as OnwWayBindingElement and CompositeDuplexBindingElement. The wsDualHttpBinding used the HttpTransportBindingElement as the binding element. It supports only the request-reply message exchange pattern.

    The wsDualHttpBinding does not support the transport level security. The encryption is not possible through the binding.

    The following code snippet shows the wsDualHttpBinding.
    Code:
    http://{hostname} : {port} / {service location }
    
    Some of the properties of the wsDualHttpBinding are as shown below:
    1. name: It is the name of the binding
    2. reliableSession: It is used to specify that the binding supports exactly one delivery assurance
    3. security: It is used to specify the security settings of the binding
    4. textEncoding: It is used to specify the way messages are sent across the network.
    5. transactionFlow: It is used to enable the flow of transactions from the client to the server
    The following code snippet demonstrates the ProductDuplexService implementation.
    Code:
    using System;
    using System.Collection.Generic;
    using System.ServiceModel;
    using System.Text;
    
    namespace InfoWCF
    {
        [ ServiceContract ( CallbackContract = typeof ( IProductServiceCallback ), 
        SessionMode = SessionMode.Required ) ]
        public interface IProductDuplexService
        {
            [ OperationContract ( IsOneWay = true ) ]
            void SendQuoteRequest ( string ProductName )
        }
        public interface IProductServiceCallback
        {
            [ OperationContract ( IsOneWay = true ) ]
            void SendQuoteRequest ( string ProductName, double cost )
        }
        [ ServiceBehavior ( InstanceContextMode = InstanceContextMode.PerSession ) ]
        public class ProductDuplexService : IProductDuplexService
        {
            public void SendQuoteRequest ( string ProductName )
            {
                int quantity;
                if ( ProductName==”Pen” )
                quantity=100;
                else if( ProductName=”Laptop” )
                quantity=20;
                else
                quantity=int.NaN;
                
                OperationContext ctx = OperationContext.Current;
                IProductServiceCallback callback = ctx.GetCallbackChannel <IProductServiceCallback> ();
                callback.SendQuoteResponse ( ProductName, quantity );
            }
        }
    }
    
    The hosting code for the service is as shown below:
    Code:
    using System;
    using System.Collection.Generic;
    using System.Text;
    using System.ServiceModel;
    
    namespace InfoWCF
    {
        internal class MyServiceHost
        {
            internal static ServiceHost myServiceHost = null;
            internal static void StartService()
            {
                myServiceHost = new ServiceHost ( typeof ( InfoWCF. ProductDuplexService ) );
                myServiceHost.Open ();
            }
            internal static void StopService()
            {
                if ( myServiceHost.State != CommunicationState.Closed )
                myServiceHost.Close();
            }
        }
    }
    
    The configuration information for the wsDualHttpBinding is as shown below:
    Code:
    <? xml version=”1.0” encoding=”utf-8” ?>
    <configuration>
    <system.serviceModel>
        <services>
         <service name=”InfoWCF.ProductDuplexSerivce” >
        <host>
        <baseAddresses>
            <add baseAddress=”http://localhost/productservice” />
        </baseAddresses>
        </host>
        <endpoint address=”” contract=”InfoWCF.IProductDuplexService” binding=”wsDualHttpBinding” />
        </service>
        </services>
    </system.serviceModel>
    </configuration>
    
    The client configuration for the wsDualHttpBinding is as shown below:
    Code:
    <?xml version=”1.0” encoding=”utf-8”?>
    <configuration>
        <system.serviceModel>
        <client>
            <endpoint address=”http://localhost/productservice” binding=”wsHttpBinding” bindingConfiguration=”SpecifyBaseClientAddress” contract=”InfoWCF.IProductDuplexService” >
            </endpoint>
        </client>
        <bindings>
        <wsDualHttpBinding>
        <binding name=”SpecifyBaseClientAddress” clientBaseAddress=”http://localhost:8001/client/” />
        </wsDualHttpBinding>
        </bindings>
        </system.serviceModel>
    </configuration>
    
    The client application contains the following code.
    Code:
    using System;
    using System.Collection.Generic;
    using System.Text;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    
    namespace InfoWCF
    {
        public class Program : IProductDuplexServiceCallback
        {
            static void Main ( string[] args )
            {
                string ProductName=”Pen”;
                waitForResponse = new AutoResetEvent ( false );
                InstanceContext callbackInstance = new InstanceContext ( new Program () ) ;
                using ( ProductDuplexServiceClient client = new ProductDuplexServiceClient ( callbackInstance ) )
                {
                    client.SendQuoteRequest ( ProductName );
                    waitForResponse.WaitOne();
                }
                Console.ReadLine();
            }
            
            public void SendQuoteResponse ( string ProductName, int cost )
            {
                Console.WriteLine ( “ {0} @${1}, ProductName, cost );
                waitForResponse.Set();
            }
        }
    }
    

    Communication using Queued Services



    WCF supports the communication through the MSMQ queues. The netMsmqBinding is used for the binding. It allows the client to post messages in the queue and the services can read the messages from the queue. User does not need any direct communication between the client and the server. There is a one – way communication. The IsOneWay = true property is set on the operation contract.

    The following code snippet shows the netMsmq binding.
    Code:
    net.msmq://{hostname}/[ private/ | [ public / ] ] {queue name}
    
    Some of the properties of the netMsmqBinding are as mentioned below:
    1. Durable: It specifies the queue is durable or volatile.
    2. exactlyOnce: It specifies that the delivery supports exactly one delivery confirmations
    3. maxRetryCycles: It the number of retries before the message is discarded
    4. retryCycleDelay: It is the time to wait between the delay cycles
    5. timeToLive: It is the length of time the messages are valid before they are expired
    The host configuration for the netMsmqBinding is as shown below:
    Code:
    <?xml version=”1.0” encoding=”utf-8”?>
    <configuration>
        <system.serviceModel>
        <client>
            <endpoint address=”net.msmq://localhost/private/productresponse” binding=”netMsmqBinding” bindingConfiguration=”NoMsmqSecurity” contract=”InfoWCF.IProductServiceResponse” name=”NetMsmqResponseClient” >
            </endpoint>
        </client>
        <services>
            <service name=”InfoWCF.ProductRequestService” >
            <endpoint 
                address=”net.msmq://localhost/private/productrequest” 
                contract=”InfoWCF.IProductRequest”
                bindingConfiguration=”NoMsmqSecurity”
                binding=”netMsmqBinding”
            />
        </services>
        
        <bindings>
        <netMsmqBinding>
        <binding name=”NoMsmqSecurity”>
        <security mode=”None” />
        </binding>
        </netMsmqBinding>
        </bindings>
        </system.serviceModel>
    <appSettings>
        <add key=”queueName” value=”.\private$\productrequest” />
    </appSettings>
    </configuration>
    
    The Client configuration for the netMsmqBinding is as mentioned below:
    Code:
    <?xml version=”1.0” encoding=”utf-8”?>
    <configuration>
        <system.serviceModel>
        <client>
            <endpoint address=”net.msmq://localhost/private/productrequest” binding=”netMsmqBinding” bindingConfiguration=”NoMsmqSecurity” contract=”InfoWCF.IProductServiceRequest” name=”NetMsmqResponseClient” >
            </endpoint>
        </client>
        <services>
            <service name=”InfoWCF.Program” >
            <endpoint 
                address=”net.msmq://localhost/private/productrespone” 
                contract=”InfoWCF.IProductResponse”
                bindingConfiguration=”NoMsmqSecurity”
                binding=”netMsmqBinding”
            />
        </services>
        
        <bindings>
        <netMsmqBinding>
        <binding name=”NoMsmqSecurity”>
        <security mode=”None” />
        </binding>
        </netMsmqBinding>
        </bindings>
        </system.serviceModel>
    <appSettings>
        <add key=”queueName” value=”.\private$\productresponse” />
    </appSettings>
    </configuration>
    

    Creating a Custom Binding



    Sometimes the preconfigured bindings do not complete the user requirements. User needs to create the customized binding to meet the requirements. The custom binding is created using the CustomBinding class. It is available in the System.ServiceModel.Channels namespace.

    The custom binding can be created in the configuration. The custom binding must use the named binding. The custom binding in the configuration is as shown below:
    Code:
    <?xml version=”1.0” encoding=”utf-8” ?>
    <configuration>
        <system.serviceModel>
        <bindings>
            <customBinding>
            <binding name=”CustomBinding”>
            <binaryMessageEncoding/>
            <udpTransport/>
            </binding>
            </customBinding>
        </bindings>
        </system.serviceModel>
    </configuration>
    
    The binding specifies an encoder and a transport. The encoder is optional. The transport specifies the default encoder.

    The Host Configuration for the customBinding is as shown below:
    Code:
    <?xml version=”1.0” encoding=”utf-8” ?>
    <configuration>
        <system.serviceModel>
        <services>
        <service name=”InfoWCF.ProductService” >
            <host>
            <baseAddresses>
            < add baseAddress=”net.tcp”//localhost/productservice” />
            </baseAddresses>
            </host>
    
        <endpoint address=”” contract=”InfoWCF.IProductService” binding=”customBinding” bindingConfiguration=”customBinding” />
        </service>
        </services>
        <bindings>
            <customBinding>
            <binding name=”customBinding” >
            <binaryMessageEncoding/>
            <tcpTransport/>
            </binding>
            </customBinding>
        </bindings>
        </system.serviceModel>
    </configuration>
    
    The client configuration for the customBinding is as shown below:
    Code:
    <?xml version=”1.0” encoding=”utf-8” ?>
    <configuration>
        <system.serviceModel>
        <client>
            <endpoint address=”net.tcp://localhost/productservice” 
            binding=”customBinding” bindingConfiguration=”customBinding” 
            contract=”InfoWCF.IProductService” >
            </endpoint>
        </client>
        <bindings>
            <customBinding>
            <binding name=”customBinding”>
            <binaryMessageEncoding>
            <tcpTransport />
            </binding>
            </customBinding>
        </bindings>
        </system.serviceModel>
    </configuration>
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice