Introduction:
In my previous two articles, you read some basic of WCF and a simple program to
develop and consume the WCF service. In this article we will develop a WCF service using the template provided by Visual Studio 2010 and consuming the service by a WPF client. So let's start step-by-step.
Step 1: Create a Database
In this example we have to create the database, so let's create one database with a table called Jobs with following scheme.
JobId Int Primary Key Identity,
[Desc]Varchar(100),
MinVal int,
MaxVal int
As stated above, our database with jobs table is ready; now it's time to start the
service application.
Step 2: Create WCF Service Application
Open Visual Studio -> New -> WCF Service Application which will create an
Iservice interface,Service.svc and Service.cs Classes but we don't want to use the default service so just delete it. Now Add -> New Item -> WCF Service Provide
the name as JobService it will create IjobService.cs,JobService.cs and
JobService.svc for you.
Step 3: Defining Interface
As per my last article on WCF the standerd way to work with WCF is define the
interface first and then implement it in the classes. So after adding new
service we have IjobService interface lest's defin it like bellow.
[ServiceContract]
public interface
IJobService
{
[OperationContract]
void DoWork();
[OperationContract]
ClsJobs ReturnJobs(int
_jobid);
[OperationContract]
string AddToJob(string
_desc, int _min, int
_max);
[OperationContract]
string UpdateJob(int
_jobid,string _desc,int
_min,int _max);
[OperationContract]
DataSet ReturnAllJobs();
}
In the above code you can see the interface defined with complex types also called
ClsJobs so just go to solution explorer and add new class called ClsJobs where
we are storing some values and returning to client on request.
Step 4: Prepare Complex type
In the above step we have added the new class called ClsJobs now it's time to first
implement this class so just create the simple properties for our database field
in clsjobs like bellow.
[DataContract]
public class
ClsJobs
{
[DataMember]
public int
JobId { get; set;
}
[DataMember]
public string
Desc { get; set;
}
[DataMember]
public int
MinVal { get; set;
}
[DataMember]
public int
MaxVal { get; set;
}
}
Now our complex type is also ready. Now it's time to prepare our JobService.cs
class which will implement the inteface IjobService i.e. Contract for our
service.
Step 5:
Now we will implement the JobService.cs class which is implementation of our
service contract IjobService. This class normally contain database operation in
our case.
public
ClsJobs ReturnJobs(int
_jobid)
{
ClsJobs obj =
new ClsJobs();
SqlConnection cn =
new SqlConnection("Data
Source=Server2;User Id=sa;Password=123;DataBase=PUBS1");
cn.Open();
string _sql =
"Select * From Jobs Where JobId=" + _jobid;
SqlCommand cmd =
new SqlCommand(_sql,
cn);
SqlDataReader dr =
cmd.ExecuteReader();
if (dr.Read())
{
obj.Desc = dr["Desc"].ToString();
obj.MinVal = int.Parse(dr["MinVal"].ToString());
obj.MaxVal = int.Parse(dr["MaxVal"].ToString());
obj.JobId = int.Parse(dr["JobId"].ToString());
}
else
obj.JobId = -1;
return obj;
}
public DataSet
ReturnAllJobs()
{
SqlConnection cn =
new SqlConnection("Data
Source=Server2;User Id=sa;Password=123;DataBase=PUBS1");
DataSet ds =
new DataSet();
SqlDataAdapter da =
new SqlDataAdapter("Select
* From Jobs", cn);
da.Fill(ds);
return ds;
}

Krishna GaradPosted Aug 22, 2013, 1:34 AM
@john http://www.c-sharpcorner.com/UploadFile/krishnasarala/step-toward-windows-communication-foundation-wcf/ http://www.c-sharpcorner.com/UploadFile/krishnasarala/step-towards-windows-communication-foundation-part-3/ http://www.c-sharpcorner.com/UploadFile/krishnasarala/steps-towards-windows-communication-foundation-part-2/
Mahesh ChandPosted Aug 21, 2013, 2:41 PM
Good point John. Krishna, it is a good idea to put links to your previous two articles in the beginning of the article.
John CooleyPosted Aug 21, 2013, 2:04 PM
Do you have links to your previous two articles?
Henka Programmer ProgrammerPosted May 11, 2013, 1:38 PM
well thanks a lot
Manish SinghPosted Dec 3, 2011, 1:11 AM
This is a really helpful for beginners and I wait next one...