Running an ASP.NET Core Application as a Windows Service
We can now run ASP.NET Core applications as Windows services with minimal effort. This blog post introduces how this process is done.
Join the DZone community and get the full member experience.
Join For Freeasp.net core 2.1 introduces a new application host for windows services. we can now run asp.net core applications as windows services with minimal effort. this blog post introduces how it is done and how to build and run windows services on asp.net core without any need for dirty hacks.
creating a default web application
we start with new default asp.net core 2.1 web application.
i didn't configure https as this is a sample application and it does nothing important.
by default, the program
class looks like this.
public class program
{
public static void main(string[] args)
{
createwebhostbuilder(args).build().run();
}
public static iwebhostbuilder createwebhostbuilder(string[] args) =>
webhost.createdefaultbuilder(args)
.usestartup<startup>();
}
now we have working default application and it's time to focus on the windows service.
running an asp.net core application as a windows service
running the application as a windows service takes some effort before we can open the browser and see it running under the service. first, we have to specify a runtime for our application, as asp.net core also supports operating systems and architectures where windows services don't run. for this, we have to modify the project file.
<project sdk="microsoft.net.sdk.web">
<propertygroup>
<targetframework>netcoreapp2.1</targetframework>
<runtimeidentifier>win7-x64</runtimeidentifier>
</propertygroup>
<itemgroup>
<packagereference include="microsoft.aspnetcore.app" />
</itemgroup>
</project>
next, add a reference to the nuget package microsoft.aspnetcore.hosting.windowsservices . this package has everything needed to run an asp.net core application as a windows service.
nb! for me, the newest version 2.1.1 of microsoft.aspnetcore.hosting.windowsservices conflicted with asp.net core 2.1.0 and i went with version 2.1.0 instead.
we also have to modify the main()
method of the program
class. in its simplest form, the main()
method looks like this.
public static void main(string[] args)
{
var pathtoexe = process.getcurrentprocess().mainmodule.filename;
var pathtocontentroot = path.getdirectoryname(pathtoexe);
var host = webhost.createdefaultbuilder(args)
.usecontentroot(pathtocontentroot)
.usestartup<startup>()
.build();
host.runasservice();
}
all we have to do now is to publish our application, register it as a windows service, and start the service.
running the application as a service or on a console
those who have built windows services before, know very well that the debugging of services can be a pain in one specific anatomical area as after building the service one has to deploy new version of it, attach a debugger, etc. there is simple way around - we make our windows service run also as a console application that is easy to run on debugger from visual studio.
we can apply the same trick also with asp.net core application that is running as windows service.
public static void main(string[] args)
{
var isservice = !(debugger.isattached || args.contains("--console"));
var pathtocontentroot = directory.getcurrentdirectory();
var webhostargs = args.where(arg => arg != "--console").toarray();
if (isservice)
{
var pathtoexe = process.getcurrentprocess().mainmodule.filename;
pathtocontentroot = path.getdirectoryname(pathtoexe);
}
var host = webhost.createdefaultbuilder(webhostargs)
.usecontentroot(pathtocontentroot)
.usestartup<startup>()
.build();
if (isservice)
{
host.runasservice();
}
else
{
host.run();
}
}
the code shown may seem a little tricky. here are my explanations:
- to run the application on the console from visual studio we control if the debugger is attached. if we want to run the application as a console application outside of visual studio we can use the
-console
argument. - when the application runs as a web application under the web server, we must use the current directory as the content root. but when the application runs as a service, we need an executable path as the content root.
- we remove the
-console
argument, as asp.net core expects all arguments to be name-value pairs.
now try to run the application from visual studio. it starts as a usual web application.
making the application run as a windows service
to make the application run as windows service we need some additional steps.
- publish the application to some folder.
- open the command line in administrative permissions.
- register the application as a windows service using t command (space after "binpath=" is mandatory)
sc create aspnetwindowsservice binpath= "path to my application exe"
- start service:
sc start aspnetwindowsservice
. - when the service starts, open the browser and navigate to http://localhost:5000 to see the web application running.
before releasing a new version of the service, the currently running instance must be stopped. for this, we can use the command sc stop aspnetwindowsservice
. to remove the service, run the following command: sc delete aspnetwindowsservice
.
wrapping up
with the new microsoft.aspnetcore.hosting.windowsservices nuget package, it is easy to run asp.net core applications as windows services. we had to modify project file a little bit and make the application's main()
method in order to understand if an application runs as a service or a console application. using this trick we are able to build a web application on visual studio and run it as a typical web application. our burden to get a web application running as windows service was minimal.
Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments