Azure

Creating A C# Microservice And Deploying It To Azure

Introduction

Within the earlier article, I talked about microservices and their benefits. In at the moment’s article, we are going to look into making a C# microservice after which deploying it to Azure as an App service. We are going to create the Worker microservice. This will likely be a Internet API resolution and will probably be examined utilizing Postman So, allow us to start.

Creating the C# microservice

Create a brand new Internet API undertaking in Visual Studio 2022 neighborhood preview version.

Creating A C# Microservice And Deploying It To Azure

Creating A C# Microservice And Deploying It To Azure

Creating A C# Microservice And Deploying It To Azure

Creating A C# Microservice And Deploying It To Azure

Run the answer to make sure all works advantageous.

Creating A C# Microservice And Deploying It To Azure

Subsequent, add three-class library tasks as beneath,

  1. Worker.Widespread (for widespread lessons and interfaces)
  2. Worker.DAL (for the info entry layer – set a undertaking reference to Worker.Widespread)
  3. Worker.BL (for the enterprise logic layer – set a undertaking reference to Worker.Widespread)

Additionally, set a undertaking reference for all tasks within the Worker.WebAPI undertaking.

Creating A C# Microservice And Deploying It To Azure

We now begin to replace the code.

For Worker.Widespread, setup is as beneath,

Creating A C# Microservice And Deploying It To Azure

Then add, the beneath code,

utilizing System.Collections.Generic;
namespace Worker.Widespread.PublicInterfaces {
    public interface IDepartmentBL {
        Record < Division > GetAllDepartments {
            get;
        }
    }
}
utilizing System.Collections.Generic;
namespace Worker.Widespread.PublicInterfaces {
    public interface IDepartmentDao {
        Record < Division > GetAllDepartments {
            get;
        }
    }
}
utilizing System.Collections.Generic;
namespace Worker.Widespread.PublicInterfaces {
    public interface IEmployeeBL {
        Record < Widespread.Worker > GetEmployeesByDepartment(int departmentId);
        Widespread.Worker ? GetEmployeesById(int employeeId);
    }
}
utilizing System.Collections.Generic;
namespace Worker.Widespread.PublicInterfaces {
    public interface IEmployeeDao {
        Record < Widespread.Worker > GetEmployeesByDepartment(int departmentId);
        Widespread.Worker ? GetEmployeesById(int employeeId);
    }
}
namespace Worker.Widespread {
    public class Division {
        public int Id {
            get;
            set;
        }
        public string Identify {
            get;
            set;
        }
    }
}
namespace Worker.Widespread {
    public class Worker {
        public int Id {
            get;
            set;
        }
        public string Identify {
            get;
            set;
        }
        public string Handle {
            get;
            set;
        }
        public string Telephone {
            get;
            set;
        }
        public string EmailAddress {
            get;
            set;
        }
        public int DepartmentId {
            get;
            set;
        }
    }
}

For Worker.DAL, setup as beneath,

Creating A C# Microservice And Deploying It To Azure

Then add, the beneath code (Be aware we’re utilizing hardcoded information. In a real-life utility we might in all probability get the info from an information retailer),

utilizing Worker.Widespread;
utilizing Worker.Widespread.PublicInterfaces;
utilizing System.Collections.Generic;
namespace Worker.DAL {
    public class DepartmentDao: IDepartmentDao {
        Record < Division > _departments;
        public DepartmentDao() {
            _departments = new Record < Division > () {
                new Division {
                    Id = 1, Identify = "Finance"
                }, new Division {
                    Id = 2, Identify = "Advertising"
                }
            };
        }
        public Record < Division > GetAllDepartments => _departments;
    }
}
utilizing Worker.Widespread.PublicInterfaces;
utilizing System.Collections.Generic;
utilizing System.Linq;
namespace Worker.DAL {
    public class EmployeeDao: IEmployeeDao {
        Record < Widespread.Worker > _employees;
        public EmployeeDao() {
            _employees = new Record < Widespread.Worker > () {
                new Widespread.Worker {
                        Id = 1, Identify = "John Doe", Handle = "Toronto, ON", Telephone = "12345", EmailAddress = "a@a.com", DepartmentId = 1
                    },
                    new Widespread.Worker {
                        Id = 2, Identify = "Jane Doe", Handle = "Etobicoke, ON", Telephone = "7891011", EmailAddress = "b@a.com", DepartmentId = 2
                    },
                    new Widespread.Worker {
                        Id = 3, Identify = "Mary Smith", Handle = "Mississauga, ON", Telephone = "22556644", EmailAddress = "c@a.com", DepartmentId = 2
                    }
            };
        }
        public Record < Widespread.Worker > GetEmployeesByDepartment(int departmentId) {
            return _employees.The place(e => e.DepartmentId == departmentId).ToList();
        }
        Widespread.Worker ? IEmployeeDao.GetEmployeesById(int employeeId) {
            return _employees.FirstOrDefault(e => e.Id == employeeId);
        }
    }
}

For Worker.BL, setup as beneath,

Creating A C# Microservice And Deploying It To Azure

Then add, the beneath code,

utilizing Worker.Widespread;
utilizing System.Collections.Generic;
utilizing Worker.Widespread.PublicInterfaces;
namespace Worker.BL {
    public class DepartmentBL: IDepartmentBL {
        personal readonly IDepartmentDao _departmentDao;
        public DepartmentBL(IDepartmentDao departmentDao) {
            _departmentDao = departmentDao;
        }
        public Record < Division > GetAllDepartments => _departmentDao.GetAllDepartments;
    }
}
utilizing Worker.Widespread.PublicInterfaces;
utilizing System.Collections.Generic;
namespace Worker.BL {
    public class EmployeeBL: IEmployeeBL {
        personal readonly IEmployeeDao _employeeDao;
        public EmployeeBL(IEmployeeDao employeeDao) {
            _employeeDao = employeeDao;
        }
        public Record < Widespread.Worker > GetEmployeesByDepartment(int departmentId) {
            return _employeeDao.GetEmployeesByDepartment(departmentId);
        }
        public Widespread.Worker ? GetEmployeesById(int employeeId) {
            return _employeeDao.GetEmployeesById(employeeId);
        }
    }
}

And at last, setup the Worker.WebAPI undertaking as beneath,

Creating A C# Microservice And Deploying It To Azure

And replace the code as beneath,

utilizing Worker.Widespread;
utilizing Worker.Widespread.PublicInterfaces;
utilizing Microsoft.AspNetCore.Mvc;
utilizing Microsoft.Extensions.Logging;
utilizing System.Collections.Generic;
namespace Worker.WebAPI.Controllers {
    [Route("api/[controller]")]
    [ApiController]
    public class DepartmentController: ControllerBase {
        personal readonly ILogger < DepartmentController > _logger;
        personal readonly IDepartmentBL _departmentBL;
        public DepartmentController(ILogger < DepartmentController > logger, IDepartmentBL departmentBL) {
                _logger = logger;
                _departmentBL = departmentBL;
            }
            [HttpGet]
        public IEnumerable < Division > Get() {
            return _departmentBL.GetAllDepartments;
        }
    }
}
utilizing Worker.Widespread.PublicInterfaces;
utilizing Microsoft.AspNetCore.Mvc;
utilizing Microsoft.Extensions.Logging;
utilizing System.Collections.Generic;
namespace Worker.WebAPI.Controllers {
    [ApiController]
    public class EmployeeController: ControllerBase {
        personal readonly ILogger < EmployeeController > _logger;
        personal readonly IEmployeeBL _employeeBL;
        public EmployeeController(ILogger < EmployeeController > logger, IEmployeeBL employeeBL) {
                _logger = logger;
                _employeeBL = employeeBL;
            }
            [HttpGet]
            [Route("api/[controller]/departments")]
        public IEnumerable < Widespread.Worker > Get(int departmentId) {
                return _employeeBL.GetEmployeesByDepartment(departmentId);
            }
            [HttpGet]
            [Route("api/[controller]/particulars")]
        public Widespread.Worker GetEmployee(int employeeId) {
            return _employeeBL.GetEmployeesById(employeeId);
        }
    }
}
utilizing Worker.BL;
utilizing Worker.Widespread.PublicInterfaces;
utilizing Worker.DAL;
utilizing Microsoft.AspNetCore.Builder;
utilizing Microsoft.Extensions.DependencyInjection;
utilizing Microsoft.Extensions.Internet hosting;
var builder = WebApplication.CreateBuilder(args);
// Add companies to the container.
builder.Companies.AddScoped < IDepartmentDao, DepartmentDao > ();
builder.Companies.AddScoped < IEmployeeDao, EmployeeDao > ();
builder.Companies.AddScoped < IDepartmentBL, DepartmentBL > ();
builder.Companies.AddScoped < IEmployeeBL, EmployeeBL > ();
builder.Companies.AddControllers();
builder.Companies.AddSwaggerGen(c => {
    c.SwaggerDoc("v1", new() {
        Title = "Worker.WebAPI", Model = "v1"
    });
});
var app = builder.Construct();
// Configure the HTTP request pipeline.
if (builder.Setting.IsDevelopment()) {
    app.UseDeveloperExceptionPage();
    app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Worker.WebAPI v1"));
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

We now run the appliance regionally and see the beneath,

Creating A C# Microservice And Deploying It To Azure

We are able to additionally check our endpoints from Postman as beneath,

Creating A C# Microservice And Deploying It To Azure

Creating A C# Microservice And Deploying It To Azure

Creating A C# Microservice And Deploying It To Azure

We are actually able to deploy the Internet APIs to Azure.

Deploying the microservice to Azure

We create an App Service in Azure as beneath,

Creating A C# Microservice And Deploying It To Azure

We then publish the code from Visual Studio.

Creating A C# Microservice And Deploying It To Azure

Creating A C# Microservice And Deploying It To Azure

Creating A C# Microservice And Deploying It To Azure

As soon as revealed, we will check our Internet APIs utilizing postman as beneath,

Creating A C# Microservice And Deploying It To Azure

Abstract

On this article, we took a take a look at making a microservice Internet API in C# and deploying it to Azure. We are able to scale this utility as required with out impacting another microservice and adjustments to it won’t have an effect on another service so long as the general public interfaces keep the identical. This design structure provides loads of flexibility to our total system design. Completely happy coding!

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button