English 中文(简体)
如果配置错,那么离Windows Service OnStart有什么正确途径?
原标题:What is the right way to exit Windows Service OnStart if configuration is wrong and nothing to do?

这是我所说的:

protected override void OnStart(string[] args)
{
    if (SomeApp.Initialize())
    {
        SomeApp.StartMonitorAndWork();
        base.OnStart(args);
    }
}

protected override void OnStop()
{
    SomeApp.TearDown();
    base.OnStop();
}

这里,如果STOP! 如果会议开始:

Timer(new TimerCallback(DoWork), null, startTime, loopTime);

和 定期调查数据库。

如果开端失败(核对记录),我试图停止从行政工具及服务中提供服务:

Could not stop the SomeService on Local Computer. The service did not return an error. 
This could be internal Windows error or an internal service error. 
If the problem persists, contact system administrator.
The question is: 
"Is exiting OnStart without doing nothing enough if Initialize returns false?

还是应该这样:

private void ExitService()
{
    this.OnStop();
    System.Environment.Exit(1);
}

protected override void OnStart(string[] args)
{
    if (ObjectFolderApp.Initialize())
    {
        SomeApp.StartMonitorAndWork();
        base.OnStart(args);
    }
    else
    {
        ExitService();
    }
}

附录

EDIT: I came up with something like this:

protected override void OnStart(string[] args)
{
    try
    {
        if (SomeApp.Initialize())
        {
            SomeApp.StartMonitorAndWork();
            base.OnStart(args);
        }
        else
        {
            Stop();
        }
    }
    catch
    {
        Stop();
    }
}

protected override void OnStop()
{
    try
    {
        SomeApp.TearDown();
        base.OnStop();
    }
    catch
    {
        base.OnStop();
    }
}
问题回答

在测试所有做法之后,我个人希望呼吁

Environment.FailFast("Configuration is wrong.");

主要目标是,未能做出解释,最终会影响到你工作的恢复环境。 因此,如果你把恢复和配置混为一谈,你的工作就会自动开始。

我知道,这并不奇怪,而是在《OnStart》中提出例外。

如果你的服务是“Autolog”服务,这还将自动向事件记录仪发出例外信息。

protected override void OnStart(string[] args)
{
    if (ObjectFolderApp.Initialize())
    {
        SomeApp.StartMonitorAndWork();
        base.OnStart(args);
    }
    else
    {
        throw new Exception("What went wrong");
    }
}

如果(<>Initialize(>>)以一些令人敏感的信息告别,但如不正确的话,我将对事件表示错误。 这是确保适当关闭服务的良好惯例。

另见, 与此相关的SO问题, 和

我建议你这样做:

protected override void OnStop()
{
    try
    {
        EventLog.WriteEntry("MyService", "Service is going to stop because of ...", EventLogEntryType.Information);        
        // Dispose all your objects here        
    }
    catch (Exception ex)
    {
        EventLog.WriteEntry("MyService", "Exception : " + ex.ToString(), EventLogEntryType.Error);                
    }
    finally
    {
        GC.Collect();                
        base.OnStop();
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签