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


Steps for creating wcf service


  •   Start VS2010 in Administrator Mode.
  • Start a Console application and wcf library/runtime to current project. And then add reference:System.ServiceModel.dll.Wcf recommends interface pattern for define contracts for users so Add New ItemàCreate interface with required methods and then add wcf attributes to it.  [ServiceContract],[OperationContract]… etc., 
  •  Once interface is defined, implement it in class with no wcf content  i.e., just like any normal .Net class extending interface.
  • Finally in main method which is starting point write code to host as well as define endpoints.
  • Execute and observe wcf service running.

A Sample Program(WCFDEMO):

1.  Write code in IDemo.cs  As follows
                            using System; 
                            using System.Collections.Generic; 
                            using System.Linq;
                            using System.Text;
                            using System.ServiceModel;

                             namespace WCFDEMO
                             {
                                         [ServiceContract]
                                        interface IDemo
                                        {
                                                 [OperationContract]
                                                   string Msg();
                                                   string Add();
                                        }
                               }
2.  Write code in Demo.cs as follow

                          using System;
                          using System.Collections.Generic;
                          using System.Linq;
                          using System.Text;

                         namespace WCFDEMO
                          {
                             public class Demo:IDemo //Extending Interface
                            {
                            public string Msg()
                            {
                               Console.WriteLine("Hello Wolrd");
                                  return "Service Created";
                            }

                            public string Add()
                            {
                               throw new NotImplementedException();
                              }
                          }
                        }    
3.  Write code in Program.cs as follow


           using System.Linq;
           using System.Text;
           using System.ServiceModel;

           namespace WCFDEMO
           {
            public class Program
            {
              static void Main(string[] args)
              {
           ServiceHost obj = new ServiceHost((typeof(Demo)));
           obj.AddServiceEndpoint(typeof(IDemo), new BasicHttpBinding(), "http://localhost:5990/sudhakarservice");
           obj.Open();
           Console.WriteLine("U R Service is Running... to Terminate enter Any Key");
           Console.ReadKey();
           obj.Close();
        }
    }
}
            
  •  Execute above WCFDEMO service.
  •  To check service is running or not in our system.
  •  Go to cmd prompt then type netstat -a 

Imp Points to Note: 

  • A service must have minimum one endpoint in our system. 
  • Every endpoint contains  Address,Binding and Contract.