0

I followed documentations and articles about Worker Service projects, and created a service that i want to host for Windows as a Windows service and for Linux as a daemon.

For hosting as a Windows Service, i need to add .UseWindowsService() (Microsoft.Extensions.Hosting.WindowsService). For hosting as a daemon, i need to add .UseSystemd() (Microsoft.Extensions.Hosting.Systemd).

What i wonder, can i use something like #if directive, and publish for windows and linux without modifiying the Program.cs ?

ahmet kocadogan
  • 127
  • 1
  • 13
  • Does this answer your question? [How to check the OS version at runtime, e.g. on Windows or Linux, without using a conditional compilation statement](https://stackoverflow.com/questions/5116977/how-to-check-the-os-version-at-runtime-e-g-on-windows-or-linux-without-using) – Guru Stron May 05 '20 at 07:40
  • 1
    Or [this](https://stackoverflow.com/questions/30153797/c-sharp-preprocessor-differentiate-between-operating-systems) one. – Guru Stron May 05 '20 at 07:42
  • @GuruStron i'll implemented the second one, will give it a try. – ahmet kocadogan May 05 '20 at 07:54
  • @GuruStron i resolved it with second comment. Thank you. – ahmet kocadogan Jun 15 '20 at 13:49
  • glad it helped! – Guru Stron Jun 15 '20 at 13:56
  • 1
    public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseWindowsService() .UseSystemd() .ConfigureServices((hostContext, services) => { services.AddHostedService(); }); } .UseWindowsService() and .UseSystemd() are NOOP so they will only work in their respective environments, So Windows one will see its not windows and just do nothing. – coolcake Jul 20 '20 at 00:07
  • @coolcake thank you for your answer. I replaced my previous code with yours. – ahmet kocadogan Jul 21 '20 at 06:47

0 Answers0