Saturday, 4 May 2013

WCF Client and Metadata | WCF Tutorial pdf

WCF Client 

WCF client is a client application creates to expose the ser vice operations as method. Any application can host a WCF client, including an application that host a service. Therefore it is possible to create a service that includes WCF clients of other services.
A client application is a managed application that uses a WCF client to communicate with another application.
To create a client application for a WCF service requires the following steps: 
1.  Get the Proxy class and service end point information Using SvcUtil.exe we can create proxy class for the service and configuration information for endpoints. Example type the following sentence in the Visual studio command prompt, this will generate the class file and configuration file which contain information
about the endpoints.
svcutil /language:vb /out:ClientCode.vb /config:app.config
http://localhost: 8090/MyService/SimpleCalculator.svc?wsdl

2.  Call operations. 
Add this class files in the client application. Then create the object for this class and invoke the service operation. Configuration information we got from the above step has to be added to the client application configuration file. When the client application calls the first operation, WCF automatically opens the under lying channel. This underlying channel is closed, when the object is recycled.
//Creating the proxy on client side 
MyCalculatorServiceProxy.MyServiceProxy proxy  
= new MyCalculatorServiceProxy.MyServiceProxy(); 
 Console.WriteLine("Counter: " + proxy.MyMethod()); 

3.  Close the WCF client object. 
After using the object created in the above steps, we have to dispose the object. Channel will be closed with the service, when the object is cleared.
Metadata 
Characteristics of the service are described by the metadata. This metadata can be exposed to the client to understand the communication with service. Metadata can be set in the service by enabling the ServiceMetadata node inside the servcieBehaviour node of the service configuration file.
<system.serviceModel> 
   <services> 
     <service name="MathService" 
       behaviorConfiguration="MathServiceBehavior"> 
       <endpoint address="" contract="IMathService" 
         binding="wsHttpBinding"/> 
     </service> 
   </services> 
   <behaviors> 
     <serviceBehaviors> 
       <behavior name="MathServiceBehavior"> 
   <serviceMetadata httpGetEnabled="True"/> 
         <serviceDebug includeExceptionDetailInFaults="true" /> 
       </behavior> 
     </serviceBehaviors> 
   </behaviors> 
 </system.serviceModel> 
This metadata can be viewed while creating WCF client application using SvcUtil.exe
The following figure illustrates the major components of WCF.

                                          Figure: WCF Architecture  
Contracts 
Contracts layer are next to that of Application layer. Developer will directly use this contract to develop the service. We are also going to do the same now. Let us see briefly what these contracts will do for us and we will also know that WCF is working on message system.
Service contracts  
- Describe about the operation that service can provide. Example, Service provided to know the temperature of the city based on the zip code, this service we call as Service contract. It will be created using Service and Operational Contract attribute.
Data contract 
 It describes the custom data type which is exposed to the client. This defines the data types, 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 datatype cannot be identified by the client e.g. Employee data type. By using Data Contract we can make client aware that we are using Employee data type for returning or passing parameter to the method.
Message Contract 
- Default SOAP message for mat 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.
Policies and Binding 
- Specify conditions required to communicate with a service e.g security requirement to communicate with service, protocol and encoding used for binding.
Service Runtime 
It contains the behaviors that occur during runtime of service. 
=>  Throttling Behavior - Controls how many messages are processed.
=>  Error Behavior - Specifies what occurs, when internal error occurs on the service.
=>  Metadata Behavior - Tells how and whether metadata is available to outside world.
=>  Instance Behavior - Specifies how many instance of the service has to be created while running.
=>  Transaction Behavior - Enables the rollback of transacted operations if a failure occurs.
=>  Dispatch Behavior  - Controls how a message is processed by the WCF Infrastructure.

Messaging 
- Messaging layer is composed of channels. A channel is a component that processes a message in some way, for example, by authenticating a message. A set of channels is also known as a channel stack. Channels are the core abstraction for sending message to and receiving message from an Endpoint. Broadly we can categories channels as
=>  Transport Channels  
Handles sending and receiving message from network. Protocols like HTTP, TCP, name pipes and MSMQ.
=>  Protocol Channels  
Implements SOAP based protocol by processing and possibly modifying message.
E.g. 
WS-Security and WS-Reliability.

Activation and Hosting 
- Services can be hosted or executed, so that it will be available to everyone accessing from the client. WCF service can be hosted by following mechanism
=>  IIS 
Internet information Service provides number of advantages if a Service uses Http as protocol. It does not require Host code to activate the service, it automatically activates service code.
=>  Windows Activation Ser vice  
(WAS)  is the new process activation mechanism that ships with IIS 7.0. In addition to HTTP based communication, WCF can also use WAS to provide message-based activation over  other protocols, such as TCP and named pipes.
=>  Self-Hosting  
WCF service can be self hosted as console application, Win Forms or WPF application with graphical UI.
=>  Windows Service  
WCF can also be hosted as a Windows Service, so that it is under control of the Service Control Manager (SCM) .

No comments: