6 March 2014

Contracts and Service Host -WCF Fundamentals

Contracts:
   
         In WCF, all services are exposed as contracts. Contract is a platform-neutral and standard way of describing what the service does. Mainly there are four types of contracts available in WCF

       
       Service Contracts describe the operation that service can provide. For Eg, a Service provide to know the temperature of the city based on the zip code, this service is called as Service contract. It will be created using Service and Operational Contract attribute.




        Data Contract describes the custom data type which is exposed to the client. This defines the data types, that are passed to and from service. Data types like int, string are identified by the client because it is already mention in XML schema definition language document, but custom created class or data types cannot be identified by the client e.g. Employee data type. By using Data Contract we can make client to be aware of Employee data type that are returning or passing parameter to the method.



  Default SOAP message format is provided by the WCF runtime for communication between Client and service. If it is not meeting your requirements then we can create our own message format. This can be achieved by using Message Contract attribute
     


 Suppose the service I consumed is not working in the client application. I want to know the real cause of the problem. How I can know the error? For this we are having Fault ContractFault Contract provides documented view for error occurred in the service to client. This helps us to easy identity, what error has occurred.      

Service Host:

        Service Host object is in the process of hosting the WCF service and registering endpoints. It loads the service configuration endpoints, apply the settings and start the listeners to handle the incoming request. System.ServiceModel.ServiceHost namespace hold this object. This object is created while self hosting the WCF service.

       In the below example you can find that WCF service is self hosted using console application.
//Creating uri for the hosting the service

  Uri uri = new Uri("http://localhost/CategoryService");

//Creating the host object for Math Service

  ServiceHost host = new ServiceHost(typeof(CategoryService), uri);

//Adding endpoint to the Host object

  host.AddServiceEndpoint(typeof(ICategoryService),new WSHttpBinding(), uri);

  host.Open(); //Hosting the Service

  Console.WriteLine("Waiting for client invocations");

  Console.ReadLine();

  host.Close();



No comments:

Post a Comment