Tuesday, 24 April 2012

Binding in WCF Services ?


Windows Communication Foundation (WCF) separates how the software for an application is written from how it communicates with other software. Bindings are used to specify the transport, encoding, and protocol details required for clients and services to communicate with each other. WCF uses bindings to generate the underlying wire representation of the endpoint, so most of the binding details must be agreed upon by the parties that are communicating. The easiest way to achieve this is for clients of a service to use the same binding that the endpoint for the service uses. For more information about how to do this, see Using Bindings to Configure Windows Communication Foundation Services and Clients.

A binding is made up of a collection of binding elements. Each element describes some aspect of how the endpoint communicates with clients. A binding must include at least one transport binding element, at least one message-encoding binding element (which the transport binding element can provide by default), and any number of other protocol binding elements. The process that builds a runtime out of this description allows each binding element to contribute code to that runtime.

WCF provides bindings that contain common selections of binding elements. These can be used with their default settings or you can modify those default values according to user requirements. These system-provided bindings have properties that allow direct control over the binding elements and their settings. You can also easily work side-by-side with multiple versions of a binding by giving each version of the binding its own name. For details, see Configuring System-Provided Bindings.

If you need a collection of binding elements not provided by one of these system-provided bindings, you can create a custom binding that consists of the collection of binding elements required. These custom bindings are easy to create and do not require a new class, but they do not provide properties for controlling the binding elements or their settings. You can access the binding elements and modify their settings through the collection that contains them. For details, see Custom Bindings.


To specify in code to use the BasicHttpBinding for the service :

1. Define a service contract for the type of service.


[ServiceContract]
public interface ICalculator
{
   [OperationContract]
   double Add(double n1, double n2);
   [OperationContract]
   double Subtract(double n1, double n2);
   [OperationContract]
   double Multiply(double n1, double n2);
   [OperationContract]
   double Divide(double n1, double n2);
}


2. Implement the service contract in a service class.

public class CalculatorService : ICalculator
{
   public double Add(double n1, double n2)
   {
      return n1 + n2;
   }
   public double Subtract(double n1, double n2)
   {
      return n1 - n2;
   }
   public double Multiply(double n1, double n2)
   {
      return n1 * n2;
   }
   public double Divide(double n1, double n2)
   {
      return n1 / n2;
   }
} 


3. In the hosting application, create the base address for the service and the binding to use with the service.

// Specify a base address for the service

String baseAddress = "http://localhost/CalculatorService";
// Create the binding to be used by the service.

BasicHttpBinding binding1 = new BasicHttpBinding();

4. Create the host for the service, add the endpoint, and then open the host.
using(ServiceHost host = new ServiceHost(typeof(CalculatorService)))
{
    host.AddServiceEndpoint(typeof(ICalculator),binding1, baseAddress);



    host.Open();
}    

To modify the default values of the binding properties ;

To modify one of the default property values of the BasicHttpBinding class, set the property value on the binding to the new value before creating the host. For example, to change the default open and close timeout values of 1 minute to 2 minutes, use the following.
TimeSpan modifiedCloseTimeout = new TimeSpan(00, 02, 00);
binding1.CloseTimeout = modifiedCloseTimeout;
If you dont want to add the service endpoint using code behind, then you can usethe Configuration method of defining service binding in web.config file as below
Create a Web.config file to configure an endpoint for the CalculatorService that uses the WSHttpBinding.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name=" CalculatorService" >
        <endpoint 
        <-- Leave the address blank to be populated by default-->
        <--from the hosting environment,in this case IIS, so -->
        <-- the address will just be that of the IIS Virtual -->
        <--Directory.-->
            address="" 
        <--Specify the binding type -->
            binding="wsHttpBinding"
        <--Specify the binding configuration name for that -->
        <--binding type. This is optional but useful if you  -->
        <--want to modify the properties of the binding. -->
        <--The bindingConfiguration name Binding1 is defined  -->
        <--below in the bindings element.  -->
            bindingConfiguration="Binding1"
            contract="ICalculator" />
      </service>
    </services>
    <bindings>
      <wsHttpBinding>
        <binding name="Binding1">
          <-- Binding property values can be modified here. -->
          <--See the next procedure. -->
        </binding>
      </wsHttpBinding>
   </bindings>
  </system.serviceModel>
</configuration>
Then Create a Service.svc file that contains the following line and place it in your Internet Information Services (IIS) virtual directory.
<%@ServiceHost language=c# Service="CalculatorService" %>


To modify the default values of the binding properties

  1. To modify one of the default property values of the WSHttpBinding, create a new binding configuration name - <binding name="Binding1"> - within the<wsHttpBinding> element and set the new values for the attributes of the binding in this binding element. For example, to change the default open and close timeout values of 1 minute to 2 minutes, add the following to the configuration file.


     <wsHttpBinding>
            <binding name="Binding1"
                     closeTimeout="00:02:00"
                     openTimeout="00:02:00">
            </binding>
          </wsHttpBinding>
    
    
    
    
Thanks For Reading..!!
Default Programmer


No comments:

Post a Comment