How To Create a WCF Service

Below is an example of creating a WCF service that interacts with an SQL database to manage employee details. First, let's start with creating a SQL table for employee details.

WCF Service Implementation

Create a new WCF Service Library project in Visual Studio.

Open the Visual Studio, create a “New Project” and select the WCF option where various WCF applications can be created. Here, I want a service application. Select that choice, name the application, and click ok.

Create project

Define the service contracts and data contracts for managing employee details.

IService1.cs (Service Contract)

IService.cs

This is the file that has all the declarations rather than a definition of properties, we call it Contract in WCF, and this helps for all the operations that happen with the service named “Operation Contract”.

Operation Contract: The method is declared, and the actual implementation is done.SVC file, each contact has to be decorated with the appropriate Attribute tags as shown below.

using System.ServiceModel;

[ServiceContract]
public interface IEmployeeService
{
    [OperationContract]
    int AddEmployee(string firstName, string lastName, string email, string department);

    // Other methods for Update, Delete, GetEmployees, etc.
}

Employee.cs (Data Contract)

Here the Data are to be transferred and processed within the service, and they store the values, so in the WCF terminology, they are called “Data Contract”. Each member of the Class, i.e., the Data contract, is called a “Data Member” and they are also to be decorated with the Attributes.

using System.Runtime.Serialization;

[DataContract]
public class Employee
{
    [DataMember]
    public int EmployeeId { get; set; }

    [DataMember]
    public string FirstName { get; set; }

    [DataMember]
    public string LastName { get; set; }

    [DataMember]
    public string Email { get; set; }

    [DataMember]
    public string Department { get; set; }
}

Implement the service contract in the service class.

EmployeeService.cs

This is the main file for any of the WCF services where this file inherits the “IService” interface and implements all the methods of the operation contract methods.

public class EmployeeService : IEmployeeService
{
    public int AddEmployee(string firstName, string lastName, string email, string department)
    {
        // Implement logic to insert employee details into the database
        int newEmployeeId = -1; // Placeholder for the new employee ID

        using (SqlConnection connection = new SqlConnection("YourConnectionString"))
        {
            string query = "INSERT INTO Employee (FirstName, LastName, Email, Department) " +
                           "VALUES (@FirstName, @LastName, @Email, @Department);" +
                           "SELECT SCOPE_IDENTITY();";

            SqlCommand command = new SqlCommand(query, connection);
            command.Parameters.AddWithValue("@FirstName", firstName);
            command.Parameters.AddWithValue("@LastName", lastName);
            command.Parameters.AddWithValue("@Email", email);
            command.Parameters.AddWithValue("@Department", department);

            connection.Open();
            newEmployeeId = Convert.ToInt32(command.ExecuteScalar());
            connection.Close();
        }

        return newEmployeeId;
    }

    // Implement other methods for Update, Delete, GetEmployees, etc.
}

Now, you can find two methods are implemented in the .SVC file.

This code demonstrates the AddEmployee method, which inserts employee details into the SQL database. Similarly, you can implement other methods for CRUD operations based on your requirements.

Database Script

Run the script below to create the Table.

CREATE TABLE Employee (
    EmployeeId INT PRIMARY KEY IDENTITY(1,1),
    FirstName NVARCHAR(50),
    LastName NVARCHAR(50),
    Email NVARCHAR(100),
    Department NVARCHAR(50)
);

Here I am using MSSQL.


Similar Articles