Thursday, May 31, 2012

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.

No comments:

Post a Comment