简体   繁体   English

在Windows服务中处理作业

[英]Processing jobs in a Windows Service

I've created a Windows Service in C# using HangFire looks like: 我使用HangFire在C#中创建了Windows服务,如下所示:

using System;
using System.Configuration;
using System.ServiceProcess;
using Hangfire;
using Hangfire.SqlServer;

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {
        private BackgroundJobServer _server;

        public Service1()
        {
            InitializeComponent();

            GlobalConfiguration.Configuration.UseSqlServerStorage("connection_string");
        }

        protected override void OnStart(string[] args)
        {
            _server = new BackgroundJobServer();
        }

        protected override void OnStop()
        {
            _server.Dispose();
        }
    }
}

I'm using VS 2017 on Windows 10. After compiling and service installed successfully but not started! 我在Windows 10上使用VS2017。编译并成功安装服务但未启动后! When I try to start manually it gives the famous Error 1053: The service did not respond to the start or control request in a timely fashion . 当我尝试手动启动时,它会发出著名的错误1053: 服务未及时响应启动或控制请求

I found an answer in stackoverflow.com regarding grant permission to NT AUTHORITY\\SYSTEM. 我在stackoverflow.com中找到了有关授予NT AUTHORITY \\ SYSTEM权限的答案。 It does not solve my problem Please help. 它不能解决我的问题,请帮助。 Thanks. 谢谢。

for Debugging use this pattern: 对于调试,请使用以下模式:

1.Add this method into WindowsService1 class: 1.将此方法添加到WindowsService1类中:

 public void OnDebug()
 {
    OnStart(null);
 }

2.In Program.cs file change the content to something like it: 2.在Program.cs文件中,将内容更改为类似的内容:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {
      #if DEBUG
        var Service = new WindowsService1();
        Service.OnDebug();
      #else
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[]
        {
            new WindowsService1()
        };
        ServiceBase.Run(ServicesToRun);
      #endif
    }
}

By this way you can run your code in User Session and check possible problems (Non User-Specific Problems) . 这样,您可以在用户会话中运行代码并检查可能的问题(非特定于用户的问题)

** Don't put all of your codes on OnStart method. **不要将所有代码都放在OnStart方法上。 The state of a service will turns to Started whenever OnStart ends. 每当OnStart结束时,服务的状态将变为Started

** Use a thread to do you works instead: **使用线程代替您的工作:

    System.Threading.Thread MainThread { get; set; } = null;
    protected override void OnStart(string[] args)
    {
        MainThread = new System.Threading.Thread(new System.Threading.ThreadStart(new Action(()=>{
            // Put your codes here ... 
        })));
        MainThread.Start();
    }

    protected override void OnStop()
    {
        MainThread?.Abort();
    }

Most of the times your error is because of this problem. 在大多数情况下,您的错误是由于此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM