Creating The WCF Services Using Visual Studio

WCF Services

Windows Communication Foundation is a programming platform and runtime system for building, configuring, and deploying the network-distributed services. It is the latest service-oriented technology. Interoperability is the fundamental characteristic of WCF.

Steps to creating the WCF Services in Visual Studio

The WCF Services can be created very easily by using the following steps.

STEP 1 - Create a database using SQL server.
  1. Database name: TestDatabase
  2. Database table name: TestRecord

TestRecord Table

img1.jpg

STEP 1 - Creating WCF Service 

Now, we can create WCF Service:

  • Go to Visual Studio >> New >> select a project.

img2.jpg

Now, click on the project, select WCF Service application, and provide a name for the Service.

img3.jpg

Now, click on the OK button. Then, you will get 3 files in Solution Explorer.

  1. IService.cs
  2. Service.svc
  3. Service.svc.cs

The following image shows the following files.

img4.jpg

For inserting the data into the database, you need to write the following code in the IService1.csfile which contains the following two sections.

  1. OperationContract
  2. DataContract

The OperationContract section is used to add the service operations and a DataContract is used to add types to the service operations.

Iservice1.cs File

Now, we create a function in the OperationContract section of the Iservice1.cs file.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8. using System.Data.SqlClient;  
  9. using System.Data;  
  10. namespace WCFServiceForInsert {  
  11.     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.  
  12.     [ServiceContract]  
  13.     public interface IService1 {  
  14.         [OperationContract]  
  15.         string InsertUserDetails(TestRecord testInfo);  
  16.         [OperationContract]  
  17.         DataSet SelectUserDetails();  
  18.         [OperationContract]  
  19.         bool DeleteUserDetails(TestRecord testInfo);  
  20.         [OperationContract]  
  21.         DataSet UpdateUserDetails(TestRecord testInfo);  
  22.         [OperationContract]  
  23.         void UpdateRegistrationTable(TestRecord testInfo);  
  24.     }  
  25.     // Use a data contract as illustrated in the sample below to add composite types to service operations.  
  26.     [DataContract]  
  27.     public class TestRecord {  
  28.         int userid;  
  29.         string username;  
  30.         string password;  
  31.         string country;  
  32.         string email;  
  33.         [DataMember]  
  34.         public int UserID {  
  35.             get {  
  36.                 return userid;  
  37.             }  
  38.             set {  
  39.                 userid = value;  
  40.             }  
  41.         }  
  42.         [DataMember]  
  43.         public string UserName {  
  44.             get {  
  45.                 return username;  
  46.             }  
  47.             set {  
  48.                 username = value;  
  49.             }  
  50.         }  
  51.         [DataMember]  
  52.         public string Password {  
  53.             get {  
  54.                 return password;  
  55.             }  
  56.             set {  
  57.                 password = value;  
  58.             }  
  59.         }  
  60.         [DataMember]  
  61.         public string Country {  
  62.             get {  
  63.                 return country;  
  64.             }  
  65.             set {  
  66.                 country = value;  
  67.             }  
  68.         }  
  69.         [DataMember]  
  70.         public string Email {  
  71.             get {  
  72.                 return email;  
  73.             }  
  74.             set {  
  75.                 email = value;  
  76.             }  
  77.         }  
  78.     }  
  79. }  

Service.svc.cs File

In this file, we defined the definition of the function InsertUserDetails(TestRecord testInfo).

Now, replace the code with the following.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8. using System.Data.SqlClient;  
  9. using System.Data;  
  10. namespace WCFServiceForInsert {  
  11.     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.  
  12.     public class Service1: IService1 {  
  13.         public DataSet SelectUserDetails() {  
  14.             SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=registration; UserID=sa; Password=wintellect");  
  15.             con.Open();  
  16.             SqlCommand cmd = new SqlCommand("Select * from TestRecord", con);  
  17.             SqlDataAdapter sda = new SqlDataAdapter(cmd);  
  18.             DataSet ds = new DataSet();  
  19.             sda.Fill(ds);  
  20.             cmd.ExecuteNonQuery();  
  21.             con.Close();  
  22.             return ds;  
  23.         }  
  24.         public bool DeleteUserDetails(TestRecord testInfo) {  
  25.             SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=registration;UserID=sa; Password=wintellect");  
  26.             con.Open();  
  27.             SqlCommand cmd = new SqlCommand("delete from TestRecord where UserID=@UserID", con);  
  28.             cmd.Parameters.AddWithValue("@UserID", testInfo.UserID);  
  29.             cmd.ExecuteNonQuery();  
  30.             con.Close();  
  31.             return true;  
  32.         }  
  33.         public DataSet UpdateUserDetails(TestRecord testInfo) {  
  34.             SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=registration;UserID=sa; Password=wintellect");  
  35.             con.Open();  
  36.             SqlCommand cmd = new SqlCommand("select * from TestRecord where UserID=@UserID", con);  
  37.             cmd.Parameters.AddWithValue("@UserID", testInfo.UserID);  
  38.             SqlDataAdapter sda = new SqlDataAdapter(cmd);  
  39.             DataSet ds = new DataSet();  
  40.             sda.Fill(ds);  
  41.             cmd.ExecuteNonQuery();  
  42.             con.Close();  
  43.             return ds;  
  44.         }  
  45.         public void UpdateRegistrationTable(TestRecord testInfo) {  
  46.             SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=registration;UserID=sa; Password=wintellect");  
  47.             con.Open();  
  48.             SqlCommand cmd = new SqlCommand("update TestRecord set UserName=@UserName,Password=@Password,Country=@Country, Email=@Email where UserID=@UserID", con);  
  49.             cmd.Parameters.AddWithValue("@UserID", testInfo.UserID);  
  50.             cmd.Parameters.AddWithValue("@UserName", testInfo.UserName);  
  51.             cmd.Parameters.AddWithValue("@Password", testInfo.Password);  
  52.             cmd.Parameters.AddWithValue("@Country", testInfo.Country);  
  53.             cmd.Parameters.AddWithValue("@Email", testInfo.Email);  
  54.             cmd.ExecuteNonQuery();  
  55.             con.Close();  
  56.         }  
  57.         public string InsertUserDetails(TestRecord testInfo) {  
  58.             string Message;  
  59.             SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=registration;UserID=sa; Password=wintellect");  
  60.             con.Open();  
  61.             SqlCommand cmd = new SqlCommand("insert into TestRecord (UserName,Password,Country,Email) values(@UserName,@Password,@Country,@Email)", con);  
  62.             cmd.Parameters.AddWithValue("@UserName", testInfo.UserName);  
  63.             cmd.Parameters.AddWithValue("@Password", testInfo.Password);  
  64.             cmd.Parameters.AddWithValue("@Country", testInfo.Country);  
  65.             cmd.Parameters.AddWithValue("@Email", testInfo.Email);  
  66.             int result = cmd.ExecuteNonQuery();  
  67.             if (result == 1) {  
  68.                 Message = testInfo.UserName + " Details inserted successfully";  
  69.             } else {  
  70.                 Message = testInfo.UserName + " Details not inserted successfully";  
  71.             }  
  72.             con.Close();  
  73.             return Message;  
  74.         }  
  75.     }  
  76. }  

Open the service in the browser. Now, right-click on the service1.vcs -> open in browser:

img6.jpg

Now, copy the URL.

img8.jpg

URL - http://localhost:2268/Service1.svc

If you see that output similar to what I have shown, you have created the WCF services successfully. Now, you can integrate this WCF Service in your application. I will be using this WCF service application in my next blog with ASP.NET and performing the CRUD operation.