Windows服务无法启动我的应用程序

时间:2017-08-15 15:04:24

标签: c# service windows-services

Long Story short我构建了一个工作正常的控制台应用程序,然后被告知它需要是一个Windows服务。所以我构建了一个Windows服务项目,几乎遵循微软的建议,服务运行,启动和停止正常,但它没有启动我从控制台应用程序转换为服务的应用程序。我无法理解为什么。

问题: 如何使用Windows服务启动长时间运行的进程? Windows服务可以不使用类库中的代码吗?

public partial class CallQService : ServiceBase

{
private static System.Threading.Timer _timer;
private static CallQ _callQ;

[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool SetServiceStatus(IntPtr handle, ref ServiceStatus serviceStatus);

public CallQService(string[] args)
{
    // Update the service state to Start Pending.  
    ServiceStatus serviceStatus = new ServiceStatus();
    serviceStatus.dwCurrentState = ServiceState.SERVICE_START_PENDING;
    serviceStatus.dwWaitHint = 100000;
    SetServiceStatus(ServiceHandle, ref serviceStatus);

    InitializeComponent();

    string eventSourceName = "MySource";
    string logName = "MyNewLog";
    if (args.Any())
    {
        eventSourceName = args[0];
    }
    if (args.Length > 1)
    {
        logName = args[1];
    }
    eventLog1 = new EventLog();
    if (!SourceExists(eventSourceName))
    {
        CreateEventSource(eventSourceName, logName);
    }
    eventLog1.Source = eventSourceName;
    eventLog1.Log = logName;

    // Update the service state to Running.  
    serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING;
    SetServiceStatus(ServiceHandle, ref serviceStatus);
}

protected override void OnStart(string[] args)
{
    Debugger.Launch();

    eventLog1.WriteEntry("The Service Has Started");

    System.Timers.Timer timer = new System.Timers.Timer
    {
        Interval = 60000 // 60 seconds  
    };
    timer.Elapsed += new ElapsedEventHandler(OnTimer);
    timer.Start();

      //This is where the process starts that is supposed to run my application and generate my data. It does not do that at all. it hits this and runs through the code in this class file, but once it is supposed to move into code in one of my class library's it just stops and doesn't do anything else.
    Initialize();
}

private void OnTimer(object sender, ElapsedEventArgs e)
{
    // TODO: Insert monitoring activities here.  
    eventLog1.WriteEntry("Monitoring the System", EventLogEntryType.Information);
}

protected override void OnContinue()
{
    eventLog1.WriteEntry("In OnContinue.");

    // Update the service state to Running.  
    ServiceStatus serviceStatus = new ServiceStatus();
    serviceStatus.dwCurrentState = ServiceState.SERVICE_CONTINUE_PENDING;
    serviceStatus.dwWaitHint = 100000;
    SetServiceStatus(ServiceHandle, ref serviceStatus);

    serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING;
    SetServiceStatus(this.ServiceHandle, ref serviceStatus);
}

protected override void OnPause()
{
    // Update the service state to Start Pending.  
    ServiceStatus serviceStatus = new ServiceStatus();
    serviceStatus.dwCurrentState = ServiceState.SERVICE_PAUSE_PENDING;
    serviceStatus.dwWaitHint = 100000;
    SetServiceStatus(ServiceHandle, ref serviceStatus);

    eventLog1.WriteEntry("The Service has Stopped.");

    // Update the service state to Running.  
    serviceStatus.dwCurrentState = ServiceState.SERVICE_PAUSED;
    SetServiceStatus(this.ServiceHandle, ref serviceStatus);
}

protected override void OnStop()
{
    // Update the service state to Start Pending.  
    ServiceStatus serviceStatus = new ServiceStatus();
    serviceStatus.dwCurrentState = ServiceState.SERVICE_STOP_PENDING;
    serviceStatus.dwWaitHint = 100000;
    SetServiceStatus(ServiceHandle, ref serviceStatus);

    eventLog1.WriteEntry("The Service has Stopped.");

    // Update the service state to Running.  
    serviceStatus.dwCurrentState = ServiceState.SERVICE_STOPPED;
    SetServiceStatus(this.ServiceHandle, ref serviceStatus);
}

public static void Initialize()
{
    var callDataRepo = CreateCallDataRepo();
    var skillsRepo = CreateSkillsRepo();

    _callQ = new CallQ(skillsRepo, callDataRepo);
    _callQ.Start();
    SetUpTimer(new TimeSpan(06, 46, 00));
}

private static ISkillsRepo CreateSkillsRepo()
{
    // Generates an instance of Config data for pulling down skills from OADB database
    ISkillsDataRepoConfig skillDataRepoConfig = new SkillsDataRepoConfig();
    // generates an instance of skills repo and pulling in call data repo OADB databse config
    ISkillsRepo skillsRepo = new SkillsRepo(skillDataRepoConfig);
    return skillsRepo;
}

private static ICallDataRepo CreateCallDataRepo()
{
    // Generates an instance of Config data for pulling down skills from OADB database
    ICallDataRepoConfig callDataRepoConfig = new CallDataRepoConfig();
    // generates an instance of call data repo and pulling in call data repo OADB databse config
    ICallDataRepo callDataRepo = new CallDataRepo(callDataRepoConfig);
    return callDataRepo;
}

private static void SetUpTimer(TimeSpan alertTime)
{
    DateTime current = DateTime.Now;
    TimeSpan timeToGo = alertTime - current.TimeOfDay;
    if (timeToGo < TimeSpan.Zero)
    {
        return;//time already passed
    }
    _timer = new System.Threading.Timer(x =>
    {
        _callQ.StopGenerators();
        _callQ.Start();
    }, null, timeToGo, Timeout.InfiniteTimeSpan);
}

}

1 个答案:

答案 0 :(得分:0)

通常,如果您有一些在服务生命周期内应存在的对象,那么将它们作为OnStart方法中的局部变量并不是一个好主意。它可能会超出范围并收集垃圾。

即,让你的timer成为班级的成员变量,看看这是否有所不同。