Home > Back-end >  How to handle windows services c# with newer versions of .NET?
How to handle windows services c# with newer versions of .NET?

Time:03-05

...

Hi all,

I'd like to create a Windows Service in c#. But I can only choose one of the .NET Frameworks:

.NET Framework 2.0
.NET Framework 3.0
.NET Framework 3.5
.NET Framework 4.7.2
.NET Framework 4.8

But what I need is .NET 6.0. But the Problem is, .NET 6.0 is allready installed and is usable for console-apps? I have two versions of console-app projekts. One with (.NET Framework) and one without. But why I only have (.NET Framework) projects for service-apps?

How do you handle services. Do you write all of you app-code into the service? Do you only use the service to run a console app?

Thanks!

CodePudding user response:

Here are a couple of examples, but the main thing is, write a console application and with a hosted service, and add .UseWindowsService() on the end of your Host builder.

https://csharp.christiannagel.com/2019/10/15/windowsservice/ https://docs.microsoft.com/en-us/dotnet/core/extensions/windows-service

public static IHostBuilder CreateHostBuilder(string[] args) =>
  Host.CreateDefaultBuilder(args)
    .ConfigureLogging(
      options => options.AddFilter<EventLogLoggerProvider>(level => level >= LogLevel.Information))
    .ConfigureServices((hostContext, services) =>
    {
      services.AddHostedService<Worker>()
        .Configure<EventLogSettings>(config =>
      {
        config.LogName = "Sample Service";
        config.SourceName = "Sample Service Source";
      });
    })
    .UseWindowsService();
  • Related