aspnetcore: host.RunAsService() throws PlatformNotSupportedException on Windows

Hello!

I’m facing a very strange issue. Currently I’m porting our .Net Framework windows service to ASP.NET Core. For the windows service part, i have been following this tutorial: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-2.1

However the host.RunAsService() throws a PlatformNotSupportedException on my windows dev machine (i didn’t had the time to test it somewhere else).

System.PlatformNotSupportedException: ServiceController enables manipulating and accessing Windows services and it is not applicable for other operating systems.
   at System.ServiceProcess.ServiceBase..ctor()
   at Microsoft.AspNetCore.Hosting.WindowsServices.WebHostService..ctor(IWebHost host)
   at Project.TerminalServer.Program.Main(String[] args) in C:\Users\user\source\repos\Project\src\Project.Server\Program.cs:line 63

The code isn’t very spectacular:

IsService = args.Contains(RUN_AS_SERVICE);
ContentRoot = IsService ? Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) : Directory.GetCurrentDirectory();
Logger = NLogBuilder.ConfigureNLog(Path.Combine(ContentRoot, NLOG_CONFIG)).GetCurrentClassLogger();
var host = CreateWebHostBuilder(args.Where(x => x != RUN_AS_SERVICE).ToArray()).Build();
                
if (IsService)
{
    Logger.Info("Starting as a service.");
    host.RunAsService();
}
else
{
    Logger.Info("Starting in console mode.");
    host.Run();
}

Info:

ASP.NET Core 2.1.2 with Microsoft.AspNetCore.Hosting.WindowsServices 2.1.1.
Windows 10.0.17134 
C:\Users\user>dotnet --version
2.1.400

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 29 (9 by maintainers)

Most upvoted comments

I forgot to copy the xxx.deps.json file and it also leads to this issue. (.NET Core 3.1)

I tried it on a second dev machine now and I got the same error. I going to setup a fresh repo now.