I am developing a .NET Core application using Visual Studio Code and the dotnet sdk on C#. I have two targets frameworks in the csproj:
<TargetFrameworks>netcoreapp2.0;net471</TargetFrameworks>
and I want to compile a console application when the target is netcoreapp2.0 and a Windows Service when the target is net471. To achieve that I added the preprocessor symbol NET471 in the code associated with the service.
I found that to make a Windows service and be able to use the InstallUtil.exe, it is necessary to make a class that inherit from ServiceBase, and other that inherit from Installer. This Installer class is in the System.Configuration.Install namespace.
The problem is that when I want to build for net471 like this:
dotnet build -f net471
it failed with this error:
MyServiceInstaller.cs(4,28): error CS0234: The type or namespace name 'Install' does not exist in the namespace 'System.Configuration' (are you missing an assembly reference?) [C:\Users\jjamardo\service-test\service-test.csproj] MyServiceInstaller.cs(9,39): error CS0246: The type or namespace name 'Installer' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\jjamardo\service-test\service-test.csproj] 0 Warning(s) 2 Error(s)
I google it a lot, but I couldn't find anything helpful.
Trying a lot of things I managed to compile it BUT using net461 as the target framework.
dotnet build -f net461
This might work, but I don't want to downgrade to 4.6.1. I tried to make a .NET Framework 4.7.1 in Visual Studio 2017 and the application works fine, so I assume that the namespace is still existing. I do not understand why using dotnet compiles with the target 4.6.1 but fails with 4.7.1.
So, the question is: how can I make a Windows service using dotnet, with net471 as the target framework. Is there any other way to make a Windows service without inherit from ServiceBase and Installer?
Thanks!