How to run a blazor app as a windows service

run-blazor-as-windows-service

It may daunting at first when you try to run a blazor app as a windows service. This article breaks it down for you step by step including code examples and images.

Steps on how to run a blazor app as windows services

Below is detailed step by step procedure you can follow to run a blazor app as a windows service. This method works for blazor server app, blazor web assembly and blazor web app.

Step 1 : Nuget Package Manager Installation

Install the following nuget package manager Microsoft.Extensions.Hosting.WindowsServices

Step 2 : Registering the service in Program.cs file

Add the following code in your program.cs file to register the package installed. If intellisense doesn’t pick it up, add the following statement, using Microsoft.Extensions.Hosting.WindowsServices; to the using statements list

builder.Host.UseWindowsService();

Step 3 : Publish the app

click build on visual studio menu and select publish. Publish your app as folder.

Step 4 : Creating Windows Service

sc create TrevExp binPath="D:\dev\FormerPro\bin\Release\net6.0\publish\WindowsGSM.exe"

Open windows terminal by searching cmd on windows search button. Copy the text above and paste it in your cmd. Replace TrevExp with the name of the service you want to create. Replace the binPath value with the path to your newly created exe file. You can access that path by clicking open folder after publishing the app.

net start TrevExp

Copy the above code to your terminal to start the service. Replace TrevExp with the name of your project. You can do it alternatively by opening task manager and navigate to services.