Running `dotnet` Processes in the Background

April 05, 2024#Software Development
Article
Author image.

Sarah Dutkiewicz, Senior Trainer

Picture this - you’re working on a .NET website and want to run front-end tests against that site. What happens when you want to run tests against the sites locally? You can’t easily run the sites and then run the tests - Visual Studio doesn’t allow for that. Oh but we can have multiple terminals and multiple dotnet commands running each of the projects. Who likes managing multiple terminal windows though? Consider this alternative:

  1. Run the website as a background process.
  2. Run your tests against the sites that are running in the background.
  3. Tear down the background processes when done.

We do this in our NimblePizza demo. Let’s talk about how we achieved that.

Introducing DotnetBackground

I come from a solid command line background in the Linux realm, so I’m very familiar with running background processes. However, I was frustrated that I couldn’t get dotnet run to run in the background so that I could work in one terminal window. I did some searching and came across DotnetBackground from Javier Tuya.

DotnetBackground allows us to launch dotnet run as background processes and makes it easy for us to kill these background processes.

To start, install this .NET tool with the following command:

dotnet tool install --global DotnetBackground

Once installed, you can launch the projects following the dotnet run command pattern but replacing dotnet with DotnetBackground.

Using DotnetBackground

In our Design Patterns for Testing webinar, I created scripts to automatically run my Blazor project in the background so that I could run my tests against the site. If I wanted to run the site from the terminal window, I could do that with this command:

dotnet run --project .\NimblePizza.Blazor\NimblePizza.Blazor.csproj --launch-profile https

Note that the process is running but it doesn’t return control to the command line. You have to stop the process with Ctrl+C. At this point, I could run dotnet test in another terminal. At the end of the day, though, I don’t have enough energy to keep track of all my terminal windows.

So I created a batch file to run front-end tests against my Blazor site that works in one terminal window.

In order to run my site in the background, I ran this command:

DotnetBackground run --project .\NimblePizza.Blazor\NimblePizza.Blazor.csproj --launch-profile https

Note: When developing and testing locally, it helps to specify the launch profile so that you are sure of what URL to use in the testing.

Once the site was started in the background, then I could run dotnet testand see my test results in that same terminal window.

At the end of testing, I wanted to make sure I stop my sites. You can use DotnetBackground to tear down those sites using the following command:

DotnetBackground kill

You can also see this tool demonstrated here:

Conclusion

The NimblePizzaDemo is a small demo showing one website in the background and running tests against it. However, if you have a website that relies on an API that is in the same solution), you could use DotnetBackground to achieve this:

  1. Run the API project in the background.
  2. Run the website project in the background.
  3. Run the tests.
  4. Run DotnetBackground kill to stop the processes created in Steps 1 and 2.

You may also have other reasons for wanting to run the sites in the background. DotnetBackground has been very helpful in making this possible. Check it out!


Copyright © 2024 NimblePros - All Rights Reserved