In
this article today I am going to explain how to create and consume the WCF
service using visual studio 2013 for beginners.
Description:
In
the previous article I have explained WCF services VS Web services in Asp.net
and Introduction to Window communication foundation (WCF) in Asp.net.
I
am using Visual studio 2013 to create the WCF service tutorial.
Implementation:
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.
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:
[ServiceContract]
public interface IService1
{
[OperationContract]
string Name(string name);
[OperationContract]
string Genre(string genre);
[OperationContract]
int Budget(int cost);
[OperationContract]
int TotalCollection(int totalcollection);
[OperationContract]
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)
{
if(budget>=tcollection)
{
return tcollection - budget;
}
else
{
return tcollection - budget;
}
}
}
Now
build and run the project.
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.
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.
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).
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:5243/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference.IService1"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
Add
a webform to website.
HTML
Markup of webform:
<table>
<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>
<tr><td></td><td>
<asp:Button ID="btnsave" runat="server" Text="Save" OnClick="btnsave_Click" /></td></tr>
</table>
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("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.
In this article we have learn to how to create and consume a WCF service tutorial for beginners. I hope you enjoyed this article.
No comments:
Post a Comment