Skip to main content

Batch start all dotnet core services

I recently started writing a bunch of Microservices for a project using dotnet core. However, I am soon running into a problem where I needed to start up a bunch of Microservices for my Single Page Application to consume. Traditionally, I will have to either start up several instances of Visual Studios or setup IIS to host all my services. Is there an easy way for me to write a batch script that can allow me to start up all these services easily? The answer is Yes! Here, I will show you how

Before we begin, one of the software design paradigm is the concept of Convention over Configuration. We will need to make sure that all of the project and directory structure of the Microservices we write follow a certain convention. In my case, I have the following structure:


[Service name]\[Service name].sln

[Service name]\[Service name]\[Service name].csproj

With this convention in place, I can now write a batch script that will iterate through a list of services to build and run each of the services desired. In the first script, we are using the "start" keyword to start a new instance of the command we are writing. In this case, the command buildRunSvc refers to a bat file we have created named this. We have 2 arguments, the first refers to the name of the service and the second refers to the port number. We assume we are hosting on localhost and the port number is provided for each of the service.

start buildRunSvc Foo1 55001
start buildRunSvc Foo2 55002

The following is the content of buildRunSvc bat file. The first 2 lines are trying to obtain the 2 arguments into their respective variables. Then, we will need to do a "dotnet restore" in order to restore all the necessary nuget dependencies. The last thing is to run the command "dotnet run" which will build and run the service project. However, we need to be in the directory where the csproj file for the service is. This is why we do the cd or change directory first.

set svc=%~1
set svc_port=%~2
dotnet restore %svc%\%svc%.sln
cd %svc%\%svc%\
SET ASPNETCORE_URLS=http://*:%svc_port% && dotnet run

I hope this helps you in your journey to automate the process where you don't have to have a hard time to start each service individually.

Comments

  1. It is very good and useful for students and developer .Learned a lot of new things from your post!Good creation ,thanks for good info .Net Online Training Hyderabad

    ReplyDelete

Post a Comment