Creating and Consuming a WCF Service

WCF Service

 
How to check if a User ID already exits or not in the database using a WCF Service.
 
In this article, I am going to discuss how to create a WCF Service and use in asp.net 3.5. You can also use this service at registration time to check duplicate data.
 

How to Create a WCF service

 
Step 1: To create new WCF Service Open new web site and select WCF service like as follows:
 
Service1.JPG 
 
Step 2: By default you will get the following page.
 
Service2.JPG 
 
Step 3: Now add the following code into Service class to get record from the database.
  1. //This function is used to get Userid from database.  
  2. public DataSet GetUserId()  
  3. {  
  4.  SqlConnection con = new SqlConnection("Data Source = Puru-SQLSERVER2005; Initial Catalog = Puru; User ID = sa; Password = wintellect;");  
  5.  string cmd = "select UserID from Registration";  
  6.  SqlDataAdapter da = new SqlDataAdapter(cmd, con);  
  7.  con.Open();  
  8.  DataSet ds = new DataSet();  
  9.  da.Fill(ds);  
  10.  con.Close();  
  11.  return ds;  
  12. }  
For example, Service.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.Text;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. public class Service: IService {  
  10.  public string GetData(int value)  
  11.  {  
  12.   return string.Format("You entered: {0}", value);  
  13.  }  
  14.  public CompositeType GetDataUsingDataContract(CompositeType composite)  
  15.  {  
  16.   if (composite.BoolValue)  
  17.   {  
  18.    composite.StringValue += "Suffix";  
  19.   }  
  20.   return composite;  
  21.  }  
  22.  //This function is used to get Userid from database.  
  23.  public DataSet GetUserId()  
  24.  {  
  25.   SqlConnection con = new SqlConnection("Data Source = Puru-SQLSERVER2005; Initial Catalog = Puru; User ID = sa; Password = wintellect;");  
  26.   string cmd = "select UserID from Registration";  
  27.   SqlDataAdapter da = new SqlDataAdapter(cmd, con);  
  28.   con.Open();  
  29.   DataSet ds = new DataSet();  
  30.   da.Fill(ds);  
  31.   con.Close();  
  32.   return ds;  
  33.  }  
  34. }  
Service.svc
  1. <%@ ServiceHost Language="C#" Debug="true" Service="Service" CodeBehind="~/App_Code/Service.cs" %>  
Step 4
 
Build the application and debug; you will get the following output.
 
Service3.JPG 
 

How to use WCF service in my application?

 
Step 5
 
If you want to use this service into different application then copy the URL of this wcf service like as http://localhost:1907/CheckValidUser_WCFService/Service.svc and add the reference of this service into an appropriate application like as
 
Add service reference.JPG
 
Paste the URL and click ok button
 
Add service reference 2.JPG 
 
Reference 3.JPG 
 
Step 6
 
And if you want to use wpf service in to same application then leave step 5. And add a new web page into first application.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CheckValidUser.aspx.cs" Inherits="CheckValidUser" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <div>  
  10.         <asp:TextBox ID="textUserId" runat="server"></asp:TextBox>  
  11.         <asp:Button ID="getUserId" runat="server" OnClick="getUserId_Click" Text="Verify" />  
  12.         <asp:Label ID="lblMessage" runat="server"></asp:Label>  
  13.     </div>  
  14.     </form>  
  15. </body>  
  16. </html>     
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. public partial class CheckValidUser : System.Web.UI.Page  
  10. {  
  11.     Service wcfService = new Service();  
  12.     protected void Page_Load(object sender, EventArgs e)  
  13.     {  
  14.     }  
  15.     protected void getUserId_Click(object sender, EventArgs e)  
  16.     {  
  17.         DataSet ds = new DataSet();  
  18.         ds = (wcfService.GetUserId());  
  19.         if (ds.Tables[0].Rows.Count > 0)  
  20.         {  
  21.             foreach (DataRow row in ds.Tables[0].Rows)  
  22.             {  
  23.                 string UserId = row["UserId"].ToString();  
  24.                 if (UserId == textUserId.Text)  
  25.                 {  
  26.                     lblMessage.Text = "Valid";  
  27.                     break;  
  28.                 }  
  29.                 else  
  30.                     lblMessage.Text = "InValid";  
  31.             }  
  32.         }  
  33.     }  
  34. }  
Output: You will get the following output.
 
output.JPG 
 
Here I am entering a valid name which exists into database.
 
outputvalid.JPG 
 
Here I am entering an invalid name which does not exist into database.
 
outputInvalid.JPG 


Similar Articles