Worker Services

As I continue looking into Project Tye the examples I’ve found so far are all around web services and APIs but what about other .NET application types? A common project template which is used for background processing is the Worker Service. A Worker Service is an application which sits without a UI and processes data on a timer or when an event is raised or a message is put on a queue etc.

Getting a worker service running with Project Tye is super easy.

Creating a Worker Service

From a command line prompt we can create a new Worker Service project by executing the following command.

dotnet new worker -o MyWorkerService

This will create a new Worker Service project with the basic templating. Once the project is created navigate into the new folder which will have been created. In this example it will be called “MyWorkerService”.

Note: the -o argument defines the output folder and name of the project which is being created.

Once we have navigated into the folder we will see the basic files from the template have been created.

PS C:\src\MyWorkerService> dir

    Directory: C:\src\MyWorkerService

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----          11/09/2020    18:11                obj
d----          11/09/2020    18:11                Properties
-a---          11/09/2020    18:11            162 appsettings.Development.json
-a---          11/09/2020    18:11            168 appsettings.json
-a---          11/09/2020    18:11            371 MyWorkerService.csproj
-a---          11/09/2020    18:11            664 Program.cs
-a---          11/09/2020    18:11            792 Worker.cs

Now from inside this folder execute the tye run command as follows:

tye run

Executing the tye run command will now build the application, if it hasn’t already been, and then proceed to start up and run the new Worker Service which we have just created.

[18:00:47 INF] Executing application from C:\src\MyWorkerService\MyWorkerService.csproj
[18:00:47 INF] Dashboard running on http://127.0.0.1:8000
[18:00:47 INF] Building projects
[18:00:50 INF] Launching service myworkerservice_1df53f0a-f: C:\src\MyWorkerService\bin\Debug\net5.0\MyWorkerService.exe
[18:00:50 INF] myworkerservice_1df53f0a-f running on process id 19992
[18:00:50 INF] Replica myworkerservice_1df53f0a-f is moving to a ready state
[18:00:50 INF] Selected process 19992.
[18:00:50 INF] Listening for event pipe events for myworkerservice_1df53f0a-f on process id 19992

Navigating to the Project Tye Dashboard on http://127.0.0.1:8000 you can now see that the worker service is running.

If you click on the view logs button you will be able to see the default timer driven log output as if you were running it in the console window.

And that’s it!

Conclusion

In this post we have looked at how we can run a .NET Worker Service based application using Project Tye.

We have seen that the process of going from an empty folder to a running Worker Service templated project is very straight forward. This can allow for developers to improve their “inner loop” of development and run multiple Worker Services and other Microservice applications locally with one command.

Any questions/comments then please contact me on Twitter @WestDiscGolf