I heard a
lot about WCF but I was not using it, so it was so far from my touch. One day I
did some RND on that and found some interesting thing. Which I have mentioned
in my previous post on WCF.
Here I m
going to explain you, how it works. Just a simple demo and you will get it
within 15 minutes. Here we go:
Step: 1
First
open Visual Studio 2008 and click file --> Select New --> Website
Step: 2
Add new Item as WCF Service and give name for
WCF (myWCF) Service
and click OK
Step: 3
Open IService.cs
and write the below code
[ServiceContract]
public interface IService
{
[OperationContract]
string
Welcome(string Name);
[OperationContract]
int
AddNumber(int a, int
b);
}
Step: 4
Open Service.cs
and write the below code
public class Service : IService
{
public int AddNumber(int a, int b)
{
int c =
a + b;
return
c;
}
public string Welcome(string
msg)
{
return "My Lovely WCF Sample " + msg;
}
}
Step: 5
Right
click on solution explorer and add ‘Add Service Reference’, write your service
URL in Address filed i.e. (http://localhost/WebSite1/Service.svc).
Click on go to check your services will show in two panel services and
operations. Now click on OK. Now your services registered successfully.
Step: 6
Open your Default.aspx.cs page and use namespace
as ‘using myWCF;’ and
write below code to access your WCF services.
myWCF.ServiceClient objWCF
= new ServiceClient();
if
(!IsPostBack)
{
//call my
lovely WCF Service
CallWCFService();
}
protected void CallWCFService()
{
//check
one by one services
//lblMessage.Text
= objWCF.Welcome("ved ");
lblMessage.Text = objWCF.AddNumber(4,
6).ToString();
}
Comments