Sunday, August 16, 2015

WCF tutorial: Create and Consume WCF Service using visual studio 2013

In this article today I am going to explain how to create and consume the WCF service using visual studio 2013 for beginners.


I am using Visual studio 2013 to create the WCF service tutorial.

To create a WCF application we have to perform the following task

How to create WCF service:
To create a WCF service open visual studio >> File >> New >> Project >>Select Visual C#>>WCF service application. Type the name of WCF service (wcfservice) and hit the Ok button.

WCF tutorial

Remove the all by default (sample) code from IService1.cs and Service1.svc file.

Open the IService1.cs file and add the below given code:

    public interface IService1
         string Name(string name);
         string Genre(string genre);
        int Budget(int cost);
        int TotalCollection(int totalcollection);
        int Profit(int budget,int tcollection);

Now open the Service1.svc
public class Service1 : IService1
        public string Name(string name)
            return name;
        public string Genre(string genre)
            return genre;
        public int Budget(int cost)
            return cost;
        public int TotalCollection(int tcollection)
            return tcollection;
        public int Profit(int budget, int tcollection)
                return tcollection - budget;             
                return tcollection - budget;
Now build and run the project.

WCF tutorial

Keep the service run don’t stop it.

How to consume WCF service:
After create a WCF service we need to consume the WCF service. To consume service I am going to create a new website.
Create a new website. Now right click on website >> go to Add >> click on Service Reference. 
WCF tutorial

Add service reference window will be open. Copy the URL of WCF service project (http://localhost:5243/Service1.svc  ) and paste it in Address input and click on Go button.

WCF tutorial

Now you see a folder App_WebReferences has been added to project.
Checkout the web.config file of project. It have the detail of ABC (Address, binding and contarct).

                <binding name="BasicHttpBinding_IService1" />
            <endpoint address="http://localhost:5243/Service1.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference.IService1"
                name="BasicHttpBinding_IService1" />

Add a webform to website.
HTML Markup of webform:
            <tr><td>Name :</td><td><asp:TextBox ID="txtname" runat="server"></asp:TextBox></td></tr>
            <tr><td>Genre :</td><td> <asp:TextBox ID="txtgenre" runat="server"></asp:TextBox></td></tr>
            <tr><td>Budget :</td><td><asp:TextBox ID="txtcost" runat="server"></asp:TextBox></td></tr>
            <tr><td>Total Collection :</td><td><asp:TextBox ID="txtcollection" runat="server"></asp:TextBox></td></tr>           
                <asp:Button ID="btnsave" runat="server" Text="Save" OnClick="btnsave_Click" /></td></tr>
Add the namespace of Service reference to code file.

using ServiceReference;

On button click write the given code:
protected void btnsave_Click(object sender, EventArgs e)
ServiceReference.Service1Client objclient = new ServiceReference.Service1Client();        
Response.Write("Name :" + objclient.Name(txtname.Text));
        Response.Write("<br/>Genre :" + objclient.Genre(txtgenre.Text));
        Response.Write("<br/>Budget :" + objclient.Budget(Convert.ToInt32(txtcost.Text))+"In Crore");
        Response.Write("<br/>Total Collection :" + objclient.TotalCollection(Convert.ToInt32(txtcollection.Text))+"In Crore");
        Response.Write("<br/>Profit/Loss :" + objclient.Profit(Convert.ToInt32(txtcost.Text), Convert.ToInt32(txtcollection.Text)));

You have been done it. Your first WCF application is ready to test. Now build the project and run.

If you want to do changes in WCF service you can but don’t forget to update the service reference in website. To update the service reference keep the service running and right click on App_Webreferences folder.
WCF tutorial

 In this article we have learn to how to create and consume a WCF service tutorial for beginnersI hope you enjoyed this article. 

No comments:

Post a Comment