Thursday, May 31, 2012

Consuming Wcf Service


  1.  First thing to consume a service is to create proxy.
  2.  Proxy is a class which client should prepare in client language based on service information.
  3. W3 standards for describing service information in WSDL[Webservice Description Language]
 We can create two types of Proxy’s:
1. On-fly Proxy.
2. Strong (or) design time proxy.

Normally code in client is written based on proxy.
  1. On-fly Proxy: On-fly proxy means Proxy created @ runtime only. It is not user-friendly but provides dynamic processing advantages.
  2. Design time proxy: Proxy is created first and code is written based on proxy. These are more friendly & more preferred also for regular service consumption.
As a wcf developer and consumer we have to handle these two tasks.i.e, Creating bindings according to requirement and creating proxy based on app properly.One proxy is created for single endpoint.

  • Previous we Developed one Service Called WCFDEMO. Now we consume that WCFDEMO service at client Side.
  • Ensure that WCFDEMO service is Running.

Consuming WCF Service example:

  • ChannelFactory<t> class is used to create On-Fly proxy.It is present in same System.ServiceModel namespace.
Steps for Consuming WCF Service :

  • Start another Microsoft  VisualStudio instance sothat we can write client program here and comminicate with running wcf service.
                    File-->new Project-->ConsoleApplication[Named As YMsgr]
  • Copy (or) Get the interface created in service to our client project [WCFDEMO]  sothat we can use ChannelFactory class for creating proxy.
  • Write code in Main,which is starting point.Code to use ChannelFactory and create On-Fly proxy class then start requesting .


     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;
     using System.ServiceModel;//add reference

    namespace YMsgr
    {
       [ServiceContract]
       interface IDemo
       {
       [OperationContract]
        string Msg();
        string Add();
    }
     
   public class Program
    {
        static void Main(string[] args)
        {
            ChannelFactory<IDemo> obj = new ChannelFactory<IDemo>(new       BasicHttpBinding(),new EndpointAddress("http://localhost:5990/sudhakarservice")));
            IDemo Proxyobj = obj.CreateChannel();//CreatesProxy
            string msg = Proxyobj.Msg();
            Console.WriteLine("Result:"+msg);
            Console.ReadKey();
         }
      }
   }

ScreenShots For Above Program:

  1. File-->new Project-->ConsoleApplication[Named As YMsgr]


    2.Add Reference System.ServiceModel for create >net enabled Program to Wcf                 enabled Program


No comments:

Post a Comment