Introduction
In the previous article, I talked about microservices and their advantages. In today’s article, we will look into creating a C# microservice and then deploying it to Azure as an App service. We will create the Employee microservice. This will be a Web API solution and it will be tested using Postman So, let us begin.
Creating the C# microservice
Create a new Web API project in Visual Studio 2022 community preview edition.





Run the solution to ensure all works fine.

Next, add three-class library projects as below,
- Employee.Common (for common classes and interfaces)
- Employee.DAL (for the data access layer – set a project reference to Employee.Common)
- Employee.BL (for the business logic layer – set a project reference to Employee.Common)
Also, set a project reference for all projects in the Employee.WebAPI project.

We now start to update the code.
For Employee.Common, setup is as below,

Then add, the below code,
using System.Collections.Generic;
namespace Employee.Common.PublicInterfaces {
public interface IDepartmentBL {
List < Department > GetAllDepartments {
get;
}
}
}
using System.Collections.Generic;
namespace Employee.Common.PublicInterfaces {
public interface IDepartmentDao {
List < Department > GetAllDepartments {
get;
}
}
}
using System.Collections.Generic;
namespace Employee.Common.PublicInterfaces {
public interface IEmployeeBL {
List < Common.Employee > GetEmployeesByDepartment(int departmentId);
Common.Employee ? GetEmployeesById(int employeeId);
}
}
using System.Collections.Generic;
namespace Employee.Common.PublicInterfaces {
public interface IEmployeeDao {
List < Common.Employee > GetEmployeesByDepartment(int departmentId);
Common.Employee ? GetEmployeesById(int employeeId);
}
}
namespace Employee.Common {
public class Department {
public int Id {
get;
set;
}
public string Name {
get;
set;
}
}
}
namespace Employee.Common {
public class Employee {
public int Id {
get;
set;
}
public string Name {
get;
set;
}
public string Address {
get;
set;
}
public string Phone {
get;
set;
}
public string EmailAddress {
get;
set;
}
public int DepartmentId {
get;
set;
}
}
}
For Employee.DAL, setup as below,

Then add, the below code (Note we are using hardcoded data. In a real-life application we would probably get the data from a data store),
using Employee.Common;
using Employee.Common.PublicInterfaces;
using System.Collections.Generic;
namespace Employee.DAL {
public class DepartmentDao: IDepartmentDao {
List < Department > _departments;
public DepartmentDao() {
_departments = new List < Department > () {
new Department {
Id = 1, Name = "Finance"
}, new Department {
Id = 2, Name = "Marketing"
}
};
}
public List < Department > GetAllDepartments => _departments;
}
}
using Employee.Common.PublicInterfaces;
using System.Collections.Generic;
using System.Linq;
namespace Employee.DAL {
public class EmployeeDao: IEmployeeDao {
List < Common.Employee > _employees;
public EmployeeDao() {
_employees = new List < Common.Employee > () {
new Common.Employee {
Id = 1, Name = "John Doe", Address = "Toronto, ON", Phone = "12345", EmailAddress = "[email protected]", DepartmentId = 1
},
new Common.Employee {
Id = 2, Name = "Jane Doe", Address = "Etobicoke, ON", Phone = "7891011", EmailAddress = "[email protected]", DepartmentId = 2
},
new Common.Employee {
Id = 3, Name = "Mary Smith", Address = "Mississauga, ON", Phone = "22556644", EmailAddress = "[email protected]", DepartmentId = 2
}
};
}
public List < Common.Employee > GetEmployeesByDepartment(int departmentId) {
return _employees.Where(e => e.DepartmentId == departmentId).ToList();
}
Common.Employee ? IEmployeeDao.GetEmployeesById(int employeeId) {
return _employees.FirstOrDefault(e => e.Id == employeeId);
}
}
}











Venkatasubbarao PolisettyPosted Sep 16, 2021, 2:25 PM
Thank you Munib Butt for writing this , good to know through how UI looks from VS 2022