Saturday, 8 October 2011

WCF Client and Metadata

WCF Client

WCF client is a client application creates to expose the service 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
  2. 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
  3. Call operations.
  4. 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 underlying 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());
  5. Close the WCF client object.
  6. 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

No comments:

Post a Comment