Introduction to Building Microservices with .NET and Docker

shah-angita - Feb 24 - - Dev Community

Microservices architecture involves designing applications as a collection of small, independent services that communicate with each other using well-defined interfaces. This approach allows for the development, testing, and deployment of individual components without affecting the entire system. When combined with Docker containers, microservices can be packaged and deployed efficiently across different environments.

Overview of .NET and Docker

.NET is a free and open-source platform that supports building a wide range of applications, including web, mobile, and desktop apps. It provides robust tools and frameworks like ASP.NET for creating web APIs, which are ideal for microservices development.

Docker is a containerization platform that allows developers to package their applications and dependencies into a single container. This container can be run on any system that supports Docker, ensuring consistent behavior across different environments.

Building Microservices with .NET

Step 1: Setting Up the Project

To start building microservices with .NET, you need to set up a new project in Visual Studio. Here’s how you can do it:

  1. Create a New Solution: Open Visual Studio and create a new solution. Choose "Blank Solution" under the "Other Project Types" section.

  2. Add Microservice Projects: Inside the solution, add new projects for each microservice. For example, you might have a ProductMicroservice and a UserMicroservice. Both should be ASP.NET Core Web API projects.

  3. Configure Project Structure: Organize your microservices into separate folders within the solution.

Step 2: Implementing Microservices

Each microservice should focus on a specific functionality. For instance:

  • ProductMicroservice: Handles product-related operations such as retrieving product details or updating product information.
  • UserMicroservice: Manages user data, including user registration and login functionality.

Step 3: Containerizing Microservices with Docker

To containerize your microservices, you will need to create a Dockerfile for each service. Here’s an example of how to do it:

Dockerfile Example for ProductMicroservice

# Use the official .NET SDK image as a base
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build

# Set the working directory in the container
WORKDIR /app

# Copy the project file into the working directory
COPY *.csproj ./

# Restore NuGet packages
RUN dotnet restore

# Copy the rest of the code into the working directory
COPY . .

# Publish the application
RUN dotnet publish -c Release -o out

# Use the official .NET runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0

# Set the working directory in the container
WORKDIR /app

# Copy the published application into the container
COPY --from=build /app/out .

# Expose port 80 for the application
EXPOSE 80

# Set the entry point for the application
ENTRYPOINT ["dotnet", "ProductMicroservice.dll"]
Enter fullscreen mode Exit fullscreen mode

Repeat this process for each microservice.

Using Docker Compose

To manage multiple containers, you can use Docker Compose. This tool allows you to define and run multi-container Docker applications.

Step 1: Create a docker-compose.yml File

In the root directory of your solution, create a docker-compose.yml file. This file will define the services and their configurations.

version: '3.5'

services:
  ProductMicroservice:
    build:
      context: ./ProductMicroservice
      dockerfile: Dockerfile
    environment:
      - CONNECTIONSTRINGS__DEFAULTCONNECTION=Data Source=192.168.3.6,1433;Initial Catalog=ProductServiceDB;User Id=*****;Password=******
    ports:
      - "4201:80"

  UserMicroservice:
    build:
      context: ./UserMicroservice
      dockerfile: Dockerfile
    environment:
      - CONNECTIONSTRINGS__DEFAULTCONNECTION=Data Source=192.168.3.6,1433;Initial Catalog=UserServiceDB;User Id=*****;Password=******
    ports:
      - "4202:80"
Enter fullscreen mode Exit fullscreen mode

Step 2: Build and Run Containers

Open a command prompt in the directory where your docker-compose.yml file is located and execute the following commands:

docker-compose build
docker-compose up
Enter fullscreen mode Exit fullscreen mode

This will build the Docker images for your microservices and start them in containers.

Using an API Gateway

In a microservices architecture, an API Gateway acts as an entry point for clients. It routes requests to the appropriate microservices. You can use Ocelot, a popular .NET API Gateway, to manage your microservices.

Step 1: Create an Ocelot Project

Add a new ASP.NET Core Web API project to your solution for the API Gateway.

Step 2: Configure Ocelot

Install the Ocelot NuGet package and configure it to route requests to your microservices.

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/{everything}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 4201
        }
      ],
      "UpstreamPathTemplate": "/product/{everything}",
      "UpstreamHttpMethod": ["GET", "POST", "PUT", "DELETE"]
    },
    {
      "DownstreamPathTemplate": "/{everything}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 4202
        }
      ],
      "UpstreamPathTemplate": "/user/{everything}",
      "UpstreamHttpMethod": ["GET", "POST", "PUT", "DELETE"]
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

This configuration routes requests from /product/* to the ProductMicroservice and from /user/* to the UserMicroservice.

Conclusion

Building microservices with .NET and Docker provides a robust and scalable architecture for complex applications. By containerizing each microservice, you can ensure consistent deployment across different environments. Using an API Gateway like Ocelot helps manage the complexity of routing requests between services. This approach allows developers to focus on individual components without affecting the entire system, making it easier to maintain and update applications over time.

For more technical blogs and in-depth information related to Platform Engineering, please check out the resources available at “https://www.improwised.com/blog/".

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .