如何使用较新版本的 .NET 处理 Windows 服务 C#?

发布于 2025-01-11 15:43:39 字数 452 浏览 2 评论 0原文

...

大家好,

我想用 c# 创建一个 Windows 服务。但我只能选择.NET Framework 之一:

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

但我需要的是.NET 6.0。但问题是,.NET 6.0 是否已安装完毕并且可用于控制台应用程序?我有两个版本的控制台应用程序项目。一种带有 (.NET Framework),一种不带有。但为什么我只有服务应用程序的 (.NET Framework) 项目?

你如何处理服务。您是否将所有应用程序代码写入服务中?您仅使用该服务来运行控制台应用程序吗?

谢谢!

...

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

短叹 2025-01-18 15:43:39

这里有几个示例,但主要的是,编写一个控制台应用程序和托管服务,并在主机构建器的末尾添加 .UseWindowsService() 。

https://csharp.christiannagel.com/2019/10/15/windowsservice/< /a>
https://learn.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();

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://learn.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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文