如何运行在服务而不是后台进程下运行的C#Windows Service应用程序?

问题描述

我有一个Windows Service应用程序,我在Visual Studio中使用C#构建。基本上,应用程序是从API服务中获取数据,然后使用SDK保存到我的计算机上安装的另一个软件中。该应用程序运行正常,但在Windows的后台进程下运行。但我希望它在服务中运行

enter image description here

这是我的program.cs main()代码

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {
        Service1 myService = new Service1();
        myService.OnDebug();
        System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
    }

    
}

在Windows服务下我可以在此处进行哪些更改?

解决方法

您需要在服务中注册.exe。

您可以在powershell中运行以下代码:

New-Service -Name "YourServiceName" -BinaryPathName <yourproject>.exe

有关更多详细信息: https://docs.microsoft.com/en-us/dotnet/framework/windows-services/how-to-install-and-uninstall-services

,

简短答案:使用“ Windows服务”模板重新创建您的项目,如下所述:

<Style x:Key="SearchTextBox" TargetType="TextBox">
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBox">
                <Border 
                    BorderBrush="{StaticResource Grey200Brush}" 
                    BorderThickness="1">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        
                        <glph:GlyphAwesome x:Name="SearchSymbol"
                            Grid.Column="0"
                            Glyph="search"
                            Margin="4"
                            FontFamily="{StaticResource MyFontFamily}"
                            Foreground="{StaticResource Grey200Brush}"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Left"
                            FontSize="13"/>

                        <ScrollViewer x:Name="PART_ContentHost"
                                      Grid.Column="1" 
                                      Margin="0" />
                    </Grid>
                </Border>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsKeyboardFocusWithin" Value="True">
                        <Setter TargetName="SearchSymbol"
                                Property="Visibilty"
                                Value="Collapsed" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

然后使用installutil或集成安装程序进行安装

更长的答案:

服务是具有特定入口点的“控制台应用程序”,因此这是创建服务的最基本的代码:

https://docs.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer

如您所见,主类继承并实现了“ ServiceBase”类,并覆盖了一些方法。主要方法是“ OnStart”(在启动服务时调用)和“ OnStop”(在停止服务时调用)。

这里还有很多其他属性和方法(或在Visual Studio中按类名按F12键):

using System.ServiceProcess;

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            this.ServiceName = "Service1";
        }

        protected override void OnStart(string[] args)
        {
        }

        protected override void OnStop()
        {
        }
    }
}

看看“主要”,您会看到它是如何工作的:

https://docs.microsoft.com/en-us/dotnet/api/system.serviceprocess.servicebase

您需要记住的几件事:

  • 服务在c:\ windows \ system32中作为基本路径执行。不要使用相对路径。

  • OnStart必须快速。请勿使用该方法执行长时间的操作。最好的做法是执行所有启动检查并启动线程。

  • 以这种方式更改主体以允许调试(显然,TestMode应该是要测试的代码):

    bool isInteractive = Environment.UserInteractive || args.Contains(“-interactive”);

    如果(isInteractive) ((Service1)ServicesToRun [0])。TestMode(); 其他 ServiceBase.Run(ServicesToRun);

  • 一旦构建了.exe文件,请使用installutil将其作为服务安装

    Install a Windows service using a Windows command prompt?