Microservices Concept Using the .NET Core Framework

Microservices Concept Using the .NET Core Framework

Learn what .NET Core is, create a microservice with .NET Core, and orchestrate multiple microservices with Docker Compose.

Microservices are a modern software architecture pattern that involves breaking down a monolithic application into smaller, independent services that can be deployed, managed, and scaled separately. This approach offers many benefits, such as improved agility, scalability, resilience, and testability. In this blog post, I will show you how to demo the microservices concept using the .NET Core framework, which is a free and open-source platform for building cross-platform applications.

What is .NET Core?

.NET Core is a general-purpose development platform that supports multiple languages (such as C#, F#, and VB.NET), multiple platforms (such as Windows, Linux, and macOS), and multiple application types (such as web, mobile, desktop, gaming, and IoT). .NET Core is designed to be modular, lightweight, and fast. It also has built-in support for developing and deploying microservices using Docker containers.

Docker is a software platform that allows you to package and run your applications in isolated environments called containers. Containers are portable, consistent, and easy to manage. You can run multiple containers on a single machine or across a cluster of machines. Docker also provides tools and services for building, sharing, and running containerized applications.

How to Create a Microservice with .NET Core

To create a microservice with .NET Core, you need to follow these steps:

  1. Install the .NET Core SDK and the Docker Desktop on your machine.
  2. Create a new ASP.NET Core Web API project using the dotnet new webapi command. This will create a simple RESTful service that returns some data in JSON format.
  3. Add a Dockerfile to your project folder. This is a text file that contains instructions for building a Docker image for your service. You can use the following example as a template:

# Use the official ASP.NET Core image as the base image
FROM mcr.microsoft.com/dotnet/aspnet:7.0

# Copy the published output of your project to the image
COPY ./bin/Release/net7.0/publish .

# Expose the port that your service listens on

# Run your service when the container starts
ENTRYPOINT ["dotnet", "MyMicroservice.dll"]

  1. Publish your project using the dotnet publish -c Release command. This will compile your code and generate the output files in the bin/Release/net7.0/publish folder.
  2. Build your Docker image using the docker build -t mymicroservice . command. This will execute the Dockerfile instructions and create an image named mymicroservice.
  3. Run your Docker container using the docker run -d -p 8080:80 --name mymicroservice mymicroservice command. This will start a container named mymicroservice from the image mymicroservice, map the port 80 of the container to the port 8080 of the host machine, and run it in detached mode.
  4. Test your service by sending a request to http://localhost:8080/weatherforecast using a browser or a tool like Postman. You should see a JSON response with some weather data.

Congratulations! You have just created your first microservice with .NET Core and Docker!

How to Orchestrate Multiple Microservices with Docker Compose

In a real-world scenario, you will likely have more than one microservice that need to communicate with each other or with external resources (such as databases or message queues). To simplify the management of multiple containers, you can use Docker Compose, which is a tool that allows you to define and run multi-container applications using a YAML file.

To use Docker Compose, you need to follow these steps:

  1. Create a docker-compose.yml file in your solution folder. This file will describe the services that make up your application, their dependencies, their configuration, and their network settings. You can use the following example as a template:

version: '3.9'

# Define your first service
# Use the image that you built previously
image: mymicroservice
# Expose the port that your service listens on
- "8080:80"
# Define any environment variables that your service needs
- ConnectionString=Server=db;Database=MyDatabase;User Id=sa;Password=MyPassword;

# Define your second service
# Use another image from Docker Hub or from your registry
image: myothermicroservice
# Link this service to the first service
- mymicroservice
# Define any environment variables that your service needs
- ApiUrl=mymicroservice

# Define a database service
# Use the official SQL Server image
image: mcr.microsoft.com/mssql/server:2019-latest
# Expose the port that the database listens on
- "1433:1433"
# Define the database credentials
- SA_PASSWORD=MyPassword

Run your application using the docker-compose up -d command. This will pull any missing images, create and start the containers, and establish the network connections between them.

Test your services by sending requests to their respective endpoints using a browser or a tool like Postman. You should see the expected responses from each service.

Congratulations! You have just orchestrated multiple microservices with Docker Compose!

In this post, I have shown you how to demo the microservices concept using the .NET Core framework. You have learned how to create, deploy, and run a single microservice using .NET Core and Docker, and how to orchestrate multiple microservices using Docker Compose. I hope you have found this post useful and informative.

Further Reading:

[Microservices Monitoring: Cutting Engineering Costs and Saving Time
A few ways fort leveraging Helios to save on engineering costs and dev time for a more resource-efficient organization…gethelios.dev](gethelios.dev/blog/cut-engineering-costs-sa.. "gethelios.dev/blog/cut-engineering-costs-sa..")

[Testing Microservices - Trace Based Integration Testing Example
Microservices architectures require a new type of testing. Here's why traditional testing fail and the new automated…gethelios.dev](gethelios.dev/blog/testing-microservices-wi.. "gethelios.dev/blog/testing-microservices-wi..")

[OpenTelemetry: A full guide
Learn all about OpenTelemetry OpenSource and how it transforms microservices observability and troubleshootinggethelios.dev](gethelios.dev/opentelemetry-a-full-guide/?u.. "gethelios.dev/opentelemetry-a-full-guide/?u..")

[10 Tools for Scaling Microservices
Scale your microservices with Helios, Prometheus, AWS Lambda, Apache Kafka, RabbitMQ, Kubernetes and moremedium.com](https://medium.com/cloud-native-daily/10-tools-for-scaling-microservices-c3aa8f83f93e "medium.com/cloud-native-daily/10-tools-for-..")

[OpenTelemetry .NET Distributed Tracing - A Developer's Guide
This article overviews the implementation of distributed tracing with OTel in dotnet, and the role of E2E observabilitygethelios.dev](gethelios.dev/blog/opentelemetry-dotnet-dis.. "gethelios.dev/blog/opentelemetry-dotnet-dis..")

Did you find this article valuable?

Support Dileep Sreepathi by becoming a sponsor. Any amount is appreciated!