dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.35k stars 9.99k forks source link

host.RunAsService() throws PlatformNotSupportedException on Windows #5934

Closed cheesi closed 3 years ago

cheesi commented 6 years ago

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 PlatformNotSupportedExceptionon 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
muratg commented 6 years ago

@cheesi Interesting. Were you able to try any other Windows machine?

Also, are you able to creaate an isolated repro app so that we can investigate on our side?

cheesi commented 6 years ago

As I already mentioned, I didn't had the time to test it on another windows machine, but i will try that now.

The code of the application is closed company source, so I'm not really able to share much of it, but I will setup a new ASP.NET Core project with windows service hosting and try to figure out, if it's the code of our application or my dev machine.

muratg commented 6 years ago

@cheesi sounds good, thanks!

cheesi commented 6 years ago

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

cheesi commented 6 years ago

I figured out the problem.

I was publishing with VS and my publish profile set to "Framework Dependent", but it seems to only work with "Self Contained".

Is there a specific reason for that?

muratg commented 6 years ago

@cheesi interesting. @Tratcher thoughts?

Tratcher commented 6 years ago

It should work either way. It sounds like the right dependency isn't getting published. We do depend on a special dependency out of the Windows Compat Pack, System.ServiceProcess.ServiceController, so if that's not getting published and you end up with a placeholder that would explain the PlatformNotSupportedExceptionon.

Do you have a project handy you can share on github? Or is it just one of the doc sample apps?

cheesi commented 6 years ago

I set up a new ASP.NET repo with windows service hosting and that worked with both publish options. Unfortunately, i got two very important tasks today, so I need to put this on hold. When i have more time, I will continue investigating this issue.

ghost commented 6 years ago

@cheesi did you find the issue what was causing, as im facing the same issue now, i tried changing to self contained still facing the same error, here im running the solution in AKS cluster, getting the same error, what is ideal setting for publishing dotnet core app on Azure AKS container.

Unhandled Exception: System.PlatformNotSupportedException: ServiceController enables manipulating and accessing Windows services and it is not applicable for other operating systems. at System.ServiceProcess.ServiceBase..ctor()

cheesi commented 6 years ago

@shivashankar-awanti sorry, I didn't had the time to investigate this error further. Currently I have so many tasks. I will share any infos here, if I know something new.

muratg commented 6 years ago

@shivashankar-awanti this bug is about Windows. I'm assuming you're running Linux containers in your AKS cluster.

ghost commented 6 years ago

@muratg yes, the containers are Linux. Is there any solution for this bug?

davidfowl commented 6 years ago

@shivashankar-awanti don't use the RunAsService method, it's specifically for Windows services. Use Run instead.

ghost commented 6 years ago

@davidfowl im using ServiceBase.Run(servicesToRun) to start the service, but same issue.

davidfowl commented 6 years ago

Why are you using ServiceBase at all on linux, don't use it, it isn't for that purpose. Just use the normal Run method.

ghost commented 6 years ago

@davidfowl under which namespace is Run method which you are talking about, as im writing in this way by creating list of services which needs to be run as a instance of ServiceBase and im looping it to run one by one. if i dont use ServiceBase, what would be other approach.

davidfowl commented 6 years ago

@shivashankar-awanti I'm not sure what you're trying to do. Are you trying to write a console application on linux?

ghost commented 6 years ago

@davidfowl yes, but normal console application works but when im trying to use ServiceBase its causing issue.

cheesi commented 6 years ago

ServiceBase is WINDOWS ONLY. It is there to interact with WINDOWS service interface. IT IS NOT FOR LINUX.

ghost commented 6 years ago

@cheesi and @davidfowl Thanks for the info, but is there any other way to implement, same as ServiceBase which supports Linux Container.

FrankPeach commented 5 years ago

@cheesi were you able to get this working as "Framework Dependent"? I am having the same issue and I did make sure that System.ServiceProcess.ServiceController.dll was being published.

cheesi commented 5 years ago

Hey @FrankPeach! Nay, sorry. As this is just my side project and I had so many other things todo, I still didn't had the time to investigate this issue.

One possibility might be to wait for .NET Core 3.0, as there will be changes regarding the build/publish tasks.

Let me know, if you got anything new.

achikhv commented 5 years ago

Got this error on ASP.NET Core 2.2 (Self Contained) after including package reference <PackageReference Include="System.Text.Encoding.CodePages" Version="4.5.1" />

When I remove it, service runs successfully.

OS is Windows Server 2008 R2

Hope that helps.

romain78 commented 5 years ago

I had the same issue, the problem was the namespace : the name of the .exe file generated contained a dot : MySolution.Test.exe (won't work) => MySolutionTest.exe (works)

cphillips83 commented 4 years ago

Just to add what solved the issue for me. I have a project like dotnet thats a CLI tool for in house devops related things. This tool uses chocolatey to install individual commands in to their own folder. It then uses some reflection and command line parsing to call in to these commands.

Since the commands are in their own folders outside of the main exe, I have used the assembly event to look for DLLs that can't be found in the main exe path. For some reason the main exe path already has a stubbed out version of System.ServiceProcess when doing a self containied deploy/publish even though it was not used anywhere. This was leading to reflection load that DLL instead of the real one in the command's folder.

Once I added the nuget for System.ServiceProcess to the main project that handles command delegation the error went away and started properly. I only came to try this based on what @Tratcher said above.

My suggestion is to provide a better error message. Prior to saying "OS not supported", perhaps it should check to see if you are on windows or a supported OS and report that this is some sort of stub library and to add the nuget reference?

lothrop commented 4 years ago

I got this error message on Windows Server 2019 with .NET Core 3.1 when I failed to copy the runtimes folder along with the self-contained executable and DLLs.

Larry57 commented 4 years ago

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

KalibGao commented 4 years ago

@Larry57 https://github.com/dotnet/aspnetcore/issues/5934#issuecomment-644003133 This works for me.

davidfowl commented 3 years ago

If this still repros using the generic host, file an issue on dotnet/runtime. I believe I saw a similar issue related to nuget packages and files being overwritten on publish (I can't find it right now).