c#中的Windows服务启动问题

时间:2015-05-21 14:55:52

标签: c# .net windows wcf

当我尝试使用nssm安装后启动Windows服务时,会显示错误

Window Service Error

已添加代码

#region Private Members 

private Timer timer1 = null; 

#endregion

#region Constructor

public Service1()
{
    InitializeComponent();
}

#endregion

#region Methods
/// <summary>
/// This function run on Windows service Start event
/// </summary>
/// <param name="args">args</param>
protected override void OnStart(string[] args)
{
    try
    {
        timer1 = new Timer();
        this.timer1.Interval = 60000;         //1 Minute=60000, 2 Minute=120000,5 Minute=300000            
        this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
        timer1.Enabled = true;
        Library.WriteErrorLog(" ============================ ");
        Library.WriteErrorLog(" Windows Service Started. ");
    }
    catch (Exception ex)
    {
        Library.WriteErrorLog(" WCF Service Error Given In OnStart() Is  : " + ex.Message);
    }
}
/// <summary>
/// OnStop event will hit when Windows service Stop
/// </summary>
protected override void OnStop()
{
    timer1.Enabled = false;
    Library.WriteErrorLog(" Windows Service Stopped. ");

}
/// <summary>
/// timer1_Tick event will call in every Timer Interval as well Window Service Started
/// </summary>
/// <param name="state"></param>
private void timer1_Tick(object sender, ElapsedEventArgs e)
{
    Library.WriteErrorLog("---------------------------------");
    Library.WriteErrorLog(" Windows Service Timer Ticked and Doing His Job Succcessfully. ");
    Library.WriteErrorLog(" Windows Service Timer Call After 1 Minute. ");
    Library.WriteErrorLog(" WCF Service Call Started. ");
    TimerCalled();
}

/// <summary>
/// This function will called in 'x' time interval. common function will call from OnElapsedTime event
/// </summary>
/// <param name="b"></param>
private void TimerCalled()
{
    try
    {
        Library.WriteErrorLog(" WCF Service Call By Using Windows Service Timer. ");

        DbChecker obj = new DbChecker();
        Boolean bl = obj.checkRecord();

        Library.WriteErrorLog(" Windows Service Timer Call End. ");
        Library.WriteErrorLog("---------------------------------");
    }
    catch (Exception ex)
    {
        Library.WriteErrorLog(" WCF Service Error Given In TimerCalled() Is : " + ex.Message);
    }
}
#endregion

当我在那时调试我的解决方案时,我没有收到任何错误或警告...服务运行完美但我认为可能是Windows服务器错误

1 个答案:

答案 0 :(得分:0)

这几乎可能是由于您编写的代码所致。我能想到接收该消息的唯一原因是:(1)未处理的异常;甚至是一个允许服务过程结束的处理过的; (2)一个服务,其行为更像是一个开始处理完成的应用程序:当它启动时,它将进行一些处理然后结束,从而产生该消息。

在代码中查找这两行的提示。希望它有所帮助!