How to create WCF service application in .Net framework 4.0 using C# & how to consume it in ASP.net?
WCF service application can be reused flexibly under different practical scenarios for various ASP.net applications. It is a very good approach to use and understand. I hope this article will be helpful for all beginners of WCF.
Creation of WCF service application :
Go to New Project -> Click on WCF installed template and Select WCF service application. Give "StudentPercentageWcfService" name to WCF service application and click on OK
This will create IService1.svc and Service1.svc alongwith Service1.svc.cs
Student data example is taken here for demo purpose. I am creating interface "getIPercentage" and adding "GetStudentTotalData" and "GetStudentPercentageData" method signature in it.
First method will calculate total of 3 subject marks and second method will calculate percentage of three marks.
In WCF, method signatures are nothing but the OperationContract and interface is the ServiceContract.
I am adding DataContract as CompositeType and creating properties inside it. Properties are datamembers.
Put following code in IService1.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace StudentPercentageWcfService
{
[ServiceContract]
public interface getIPercentage
{
[OperationContract]
int GetStudentTotalData(int sub1marks, int sub2marks, int sub3marks);
[OperationContract]
float GetStudentPercentageData(int sub1marks, int sub2marks, int sub3marks);
}
[DataContract]
public class CompositeType
{
int _studentid ;
string _studentname ;
int _sub1marks ;
int _sub2marks ;
int _sub3marks ;
int _total;
float _per;
[DataMember]
public int studentid
{
get { return _studentid; }
set { _studentid = value; }
}
[DataMember]
public string studentname
{
get { return _studentname; }
set { _studentname = value; }
}
[DataMember]
public int sub1marks
{
get { return _sub1marks; }
set { _sub1marks = value; }
}
[DataMember]
public int sub2marks
{
get { return _sub2marks; }
set { _sub2marks = value; }
}
[DataMember]
public int sub3marks
{
get { return _sub3marks; }
set { _sub3marks = value; }
}
[DataMember]
public int total
{
get { return _total; }
set { _total = value; }
}
[DataMember]
public float percentage
{
get { return _per; }
set { _per = value; }
}
}
}
Put following code in Service1.svc.cs in which actual implementation is added.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace StudentPercentageWcfService
{
public class Service1 : getIPercentage
{
public int GetStudentTotalData(int sub1marks, int sub2marks, int sub3marks)
{
int total = sub1marks + sub2marks + sub3marks;
return total ;
}
public float GetStudentPercentageData(int sub1marks, int sub2marks, int sub3marks)
{
int total = sub1marks + sub2marks + sub3marks;
float percentage = (total * 100) / 300;
return percentage ;
}
}
}
Creation of ASP.net application and consume WCF service in it.
I have given name as Student to ASP.net application.
Put following code in Default.aspx
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
</h2>
<p>
<asp:Label ID="lblSub1" runat="server">Enter subject1 marks</asp:Label>
<asp:TextBox runat="server" ID="txtsub1"> </asp:TextBox><br />
<asp:Label ID="lblSub2" runat="server">Enter subject2 marks</asp:Label>
<asp:TextBox runat="server" ID="txtsub2"> </asp:TextBox><br />
<asp:Label ID="lblSub3" runat="server">Enter subject3 marks</asp:Label>
<asp:TextBox runat="server" ID="txtsub3"></asp:TextBox>
<br />
<asp:Button ID="btnCalculate" runat="server" onclick="btnCalculate_Click" Text="Submit" />
</p>
<p>
<asp:Label ID="lblTotal" runat="server"></asp:Label><br />
<asp:Label ID="lblPercentage" runat="server"></asp:Label>
</p>
</asp:Content>
Put following code in Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void btnCalculate_Click(object sender, EventArgs e)
{
try
{
StudentPercentageWcfService.Service1 src = new StudentPercentageWcfService.Service1();
lblTotal.Text = "Total of marks : " + Convert.ToString(src.GetStudentTotalData(Convert.ToInt32(txtsub1.Text), Convert.ToInt32(txtsub2.Text), Convert.ToInt32(txtsub3.Text)));
lblPercentage.Text = "Percentage of marks : " + Convert.ToString(src.GetStudentPercentageData(Convert.ToInt32(txtsub1.Text), Convert.ToInt32(txtsub2.Text), Convert.ToInt32(txtsub3.Text)));
}
catch (Exception)
{
}
}
}
Right click on solution and go to add reference and browse "StudentPercentageWcfService.dll" which gives reference of WCF service application.
Now execute the application. Enter subject marks in three textboxes. Click on Submit button to get total and percentage of marks. These are calculated by WCF service which we have consumed in ASP.net application.