Friday, 19 July 2019

Sample .Net Core Service Hosting

Windows Service, Linux daemon, Google Cloud Compute Engine

Today I am going to look at implementing .Net Core Generic Service Host and host it in .Net Core app to run as daemon under Linux. We will use Google.Cloud.Logging.NLog library to integrate NLog with Stackdriver. See "Integrating NLog with Stackdriver for error reporting on Google Cloud" for more details. App will be deployed on Google Cloud Compute Engine VM running Linux and will run as daemon service. In this sample service I will run a thread that will post an information message to Google Logging Interface. The sample code for this project is hosted on github.

We will create sample_service_hosting .Net core console project, which will implement our SampleService that will post messages to Google Logging Interface.

1. Create sample_service_hosting .Net Core console project

>mkdir sample_service_hosting
>cd sample_service_hosting
>dotnet new console
Add the following package references

NLog, NLog.Extensions.Hosting and Google.Cloud.Logging.NLog are also added for a final stage of the project to support logging messages to Google Cloud Log Viewer.

2. Implement HostBuilder configuration

Now we are going to implement HostBuilder, which will run our Generic Host with RunAsync call. I normally create it in the separate CreateHostBuilder function. It will return null if for some reason we failed to create our generic host and in Main we will need to check for returned result.
You notices that we adding appsettings.json file as well. Lets add it to our project.And a SampleService class, which we will extend in the next section. Add SampleService.cs under Services folder.

3. Implement SampleService hosted service

In this section we are going to inject nlog into our generic host builder and extend our SampleService host to log message every 10 seconds. We already configured HostBuilder to inject logger in ConfigureLogging section. We need to add .UseNLog() call just after var builder = new HostBuilder() in CreateHostBuilder function. In the SampleService class we will use CancellationTokenSource to cancel our service and stop Run thread when IsCancellationRequested is true. Run function will be called asynchronously and pointer to Task object is saved.
Logger is logging Trace messages and information messages within iteration loop. Add nlog.config to project.

4. Enabling gradual stop of the service on SIGTERM signal

We can deploy this sample_service_hosting app to Linux VM and make it run as a daemon, but with its current implementation it will not properly respond to SIGTERM sent from Linux host to gradually stop and clean up used resources. For this we are going to use injected IApplicationLifetime object and register events ApplicationStarted, ApplicationStopping and ApplicationStoped.

5. NLog.config - Add support for Stackdriver logging

At the start we already added reference to Google.Cloud.Logging.NLog package. Now we need to update nlog.config file to include assembly Google.Cloud.Logging.NLog and GoogleStackdriver target to log to Google Logging Interface.


Our sample_service_hosting app is done. Check the final version of this project on github. I am not going to cover here setup of this app as a daemon on Linux VM Google Cloud Compute Engine. This will be a topic of separate blog post. Stay tuned. 


No comments:

Post a Comment