What is WCF
WCF is Windows Communication Foundation and we can say it the elder brother of Web Service.
We will create two projects:
- Web Application.
- WCF Service Library.
Note: Within one solution we can create n number of projects and this is the advantage of Solution.
- Create Web Application.
File, New, Project, then ASP.NET Empty Web Application.

Give the name WCF_TestingWebApplication.
- Create another project of WCF Service Library under same solution.

Give name MyWCFServiceLibrary
- After creating Wcf ServiceLibrary project, You can see default two service base file created:
- IService.cs
- Service1.cs
Delete these two default created service files.
- Now, add new service item. You can add new service by the following,

- Select WCF Service item and give name FriendService,

- After creating FriendService you can see two files get created,
- IFriendService.cs: Interface to declare your Service contracts and Data Contracts.
- FriendService.cs: Is a normal class inherited by IFriendService where you can define all the methods and other processes.
- You can see both files having one DoWork() kind of thing. You can comment out or remove or let it be. This is by default.
- Namespace required,
Namespace Functionalities Using System.Data To get ado.net data handling classes. Using System.Data.SqlClient To connect to Microsoft Sql server. Using Systen.Configuration To fetch configuration settings and connectionstrings from app.config. Firstly, give reference of system.configuration.dll. - Now we will define our function name GetFriendNameBySearchKeywords(). Firstly, we have given signature of this function in IFriendService.cs then Implemented in FriendService.cs.
Double click on IFriendService.cs file.
Write the following codes just above DoWork().
In gray color code we define method GetFreindNameBySearchKeywords as per Friend class declaration.- [ServiceContract]
- public interface IFriendService
- {
- [OperationContract]
- Friend GetFriendNameBySearchKeywords();
- [OperationContract]
- void DoWork();
- }
In orange color code we defined implementation of Friend class.- [DataContract]
- public class Friend
- {
- public Friend()
- {
- this.FriendsTable = new DataTable("FriendsTable");
- }
- [DataMember]
- public DataTable FriendsTable
- {
- get;
- set;
- }
- }

- By default WCF service library application give you APP.CONFIG file.
Insert Connection String inside CONFIGURATION tag.
- <connectionStrings>
- <add name="MemberCDACConnectionString" connectionString="Data Source=SAIBABA-PC\SAIBABA;Initial Catalog=MemberCDAC;Integrated Security=True" providerName="System.Data.SqlClient"/>
- </connectionStrings>
- Table structure of tblFriends,
The sample records of tblFriends,- CREATE TABLE [dbo].[tblFriends](
- [FriendID] [int] IDENTITY(1,1)NOT NULL,
- [FriendName] [varchar](50)NULL,
- [FriendImage] [varchar](500)NULL,
- [Place] [varchar](500)NULL,
- [Mobile] [varchar](20)NULL
- )ON [PRIMARY]
- GO

- Now, we implement GetFriendNameBySearchKeywords() method in FriendService.cs.
FriendService.cs, this file inheriting interface file IFreindService.
Here we are implementing our method GetFreindNameBySearchKeywords.
- public class FriendService: IFriendService
- {
- //To fetch connection string from APP.CONFIG file.
- string ConStr = ConfigurationManager.ConnectionStrings["MemberCDACConnectionString"].ConnectionString;
- public Friend GetFriendNameBySearchKeywords(string FriendSearchKeyword)
- {
- //Set connection which feteched from app.config.
- SqlConnection con = new SqlConnection(ConStr);
- //SELECT query to fetch data from ProductsTable
- SqlDataAdapter da = new SqlDataAdapter("Select FriendName From tblFriends Where ProductName Like '%" + FriendSearchKeyword + "%'", ConStr);
- //DataSet is virutual database or data container.
- Friend _friends = new Friend();
- //Fill data inside ds(DataSet) with TableNamed ProductsTable
- da.Fill(_friends.FriendsTable);
- return _friends;
- }
- public void DoWork()
- {}
- }

We had completed WCF work, check and build your WCF Service Library project.
If you have any error solve or cannot you can comment I will answer as early as possible.



Join the conversation! Your thoughts help the community grow.