Follow in Twitter & Facebook

Like in Facebook

Powered By Blogger Widgets

Free Download

FREE Tools

Friday, April 6, 2012

Wcf Implementation Example in VisualStudio 2010.

Here we will see how the client will consume the WCFservice with a small example by visual studio 2010. You can check a very good article on how to create wcf and host wcf service in windows service in .Net.

First create a project by following path
File->New->Project->Then choose C# and click WCF tab in left side->Then Choose WCF Service Application. Then give project name as WcfSecondSample then click ok.

Now your project is created then it will create defaultly 3 files like service1.svc,IService1.cs and one web.config file. so if you want you can change the name of the interface (IService1.cs) and the classfile (Service1.svc). But for the timing i am not changing now.Let be the names as it is.
Then open the IService1.cs declare the method GetEmpDetails() and it will take a custom class object as a parameter.

I declared that custom class below to the Interface with [DataContract] attribute for that custom class and [Datamember] attribute for the inside members of the same custom class and i get and set the properties there .
The full code looks like belows

    public interface IService1
        string GetEmpDetails(Employee emp);

    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    public class Employee
        string EmpName = "";
        int  EmpID = 10;

        public string EmployeeName
            get { return EmpName; }
            set { EmpName = value; }
        public int EmployeeID
            get { return EmpID; }
            set { EmpID = value; }

Then open that Service1.svc.cs,This is the implementation class for that interface.
Then write as belows.
  public class Service1 : IService1
      public  string GetEmpDetails(Employee emp)
            emp.EmployeeName = "Raju";
            emp.EmployeeID = 12;
            string output = "The emploee name and id is " +emp.EmployeeName+emp.EmployeeID;
            return output;
Now your coding for that wcf service is over then before handling this service to the client you need to test your service.You can test your service  by the help of WcfTestclient tool.
In order to test your service just open your visual studio 2010 command prompt thn go to the path like below

C:/Programfiles/visualstudio 10.0/Common7/IDE wcfTestclient http://localhost:50882/Service1.svc

Here  C:/Programfiles/visualstudio 10.0/Common7/IDE is the Path of wcfTestclient.
          http://localhost:50882/Service1.svc  is the Path of the service i.e right click on that service1.svc file then select view in browser and copy that path from url.
Then you can able to see your method and you can test the functionality by giving one value for Employeeid and name.

Then now your's Service is also ok so you can handle to the client srevice work is over now we need to create a client so that that service can consume our service.
In order to create the client right click on the same solution ->Add New website -> web Application then give the name as WCFSecondSampleClient then click ok.

Now your client is prepaired then this is the time to add the service reference.
For that right click on the client project in my case its WCFSecondSampleClient then select Add serviceReference then one window will open click discover button it will find the service then below to that one NameSpace option is there Change the name to any other name .In my case its MyProxy . Remember this name because its very important.Then click ok.
Then open the Default.aspx.cs and write in the page load like

 protected void Page_Load(object sender, EventArgs e)
        MyProxy.Service1Client sobj = new MyProxy.Service1Client();
        MyProxy.Employee obj = new MyProxy.Employee();
Here MyProxy is the Namespace name (That namespace name which we have given while adding that service reference).

Service1Client it will automatically come when you give that symbol dot(.) usually it will come as the name as your servicenameclient.

In second row Employee is the custom class that we have created in that IService1.cs.
You need to instanciate two times like this above. Then right click on client project (WCFSecondSampleClient in our case) and choose the option Set as startupProject.
Then now every thing done you can run the application and see the result. You can also check some WCF articles here.

0 on: "Wcf Implementation Example in VisualStudio 2010."