host.RunAsService() throws PlatformNotSupportedException on Windows

See original GitHub issue

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

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
Larry57commented, Jun 15, 2020

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

1reaction
cheesicommented, Aug 28, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hosting ASP.NET Core as Windows service
In this example, I'm going to create a Console Application that uses Microsoft.DotNet.Web.targets, outputs an .exe and operates as a MVC app ( ......
Read more >
Developers - host.RunAsService() throws ...
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 ...
Read more >
Running a .NET Core Generic Host App as a Windows Service
In this post we'll explore how to take a .NET Core console application, running on the Generic Host, utilising IHostedService and deploy it ......
Read more >
Create Windows Service using BackgroundService - .NET
Learn how to create a Windows Service using the BackgroundService in .NET.
Read more >
Can't start ASP.NET Core 6 app running as Windows Service
NET Core 6 WebAPI project, hosted as a Windows Service, just trying to ... Host.UseWindowsService(); builder.WebHost.UseUrls(UriServices.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found