Using TopShelf for windows service

Windows service runs continuously in the background and more often used as a back-end job. Its a pain if you want to debug it,even though you use logger to capture all events.we either attach the service as a process and debug it. But TopShelf lets you eliminate all of these and allows you to debug like console  application.

Topshelf has a start and stop method just like a service and also an separate command to install windows service with ease.

DocLink : https://topshelf.readthedocs.io/en/latest/installation/index.html

 

A simple use case with OData Web Api

Scenario :

Assume a case where we have an Employee object and we have an nullable  integer named Age (int? Age).  There are two cases we want to achieve,the first case is to not update the Age property and we could achieve this by passing null value to Age and ignoring the Age Property while saving and second case is to reset the Age i.e Accept whatever is passed by Age Property and save it in db.

Solution :

We want to only update properties of Employee object that is being passed to the controller and we call it as “partial update”(PATCH).  In this case we update only what is being passed and leave the system unchanged. Microsoft provides OData for partial update.

  1. Get Microsoft ASP.NET Web API OData package from Nuget.
  2. In your controller  HTTP verb Method,you could make use of Delta<T>  to map your Employee object and this will take care of partial update with Patch Method provided by ODATA .

public ApiResponse<Statuscode> Patch(Delta<Employee>; employee)

{

var employeebyId == dbcontext.FirstorDefault(x=>x.EmpId ==employee.EmpId);

if(employee !=null)

{

employee.Patch(employeebyId ); // Partial update by ODATA

}

}