b

Wednesday, 9 May 2012

Step By Step Process to develop Simple WCF Service

Step 1)  Create WCF Service Application as shown below




It will create WcfService1 project
                     All files
                 IService1.cs --> interface declaration
                 Service1.svc --> WCF service activation code & service implementation code.
             Delete these 2 files

Step2)   Now Add  new 3 files
              1) MyInterface.cs
              2)MyInterfaceImpl.cs
              3) ServiceHostone.svc

    2.1) Creating interface file(MyInterface.cs) as shown below
 
FileName: MyInterface.cs



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WcfService1
{
   
    [ServiceContract]
    interface MyInterface
    {
        [OperationContract]
        int Add(int x, int y);


        [OperationContract]
        string getUsername();
        
    }
}


 2.2)  Creating  new class  MyInterfaceImpl.cs. This is interface/service implementation file

FileName:MyInterfaceImpl.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WcfService1
{
    public class MyInterfaceImpl : MyInterface
    {
        public int Add(int x, int y)
        {
            return x + y;
        }

        public string getUsername()
        {
            return "My Name is john peter paul";
        }
    }
}


3.1) Create  SVC file  , for hosting WCF service in IIS hosting configuration.
     1)  Create text file name it as  ServiceHostone.svc

    in   ServiceHostOne.SVC

add following statement
                 <%@ ServiceHost Service="WcfService1.MyInterfaceImpl" %>

In this  WcfService1 is a  namespace
            MyInterfaceImpl is a interface implemented class,


Step 3) Build the Project/ F5 
           It will launch the WcfTestClient where our service will be loaded automatically

           3.1) Double click on each method.
           Right side  for Add method requires two params x & y  enter those value --> click on Invoke
result is shown below.




   3.2) Do Same thing for getUsername method.

That's it.

1 comment: