System.NullReferenceException 启动期间 Windows 服务出错
System.NullReferenceException error in Windows Service during start-up
在尝试启动我的 Windows 服务时,当状态栏快到 50% 时突然出现以下错误:
Error 1053: The service did not respond to the start or control request in timely fashion
当我检查事件查看器时,我看到以下错误:
Application: EnvMonService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception
Exception Info: System.NullReferenceException
Stack: at EnvMonService.EnvMonService..ctor() at EnvMonService.Program.Main()
我看不出错误的来源 - 这是我的代码:
主要()
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new EnvMonService()
};
ServiceBase.Run(ServicesToRun);
}
构造函数
public EnvMonService()
{
InitializeComponent();
}
OnStart 和 OnStop
protected override void OnStart(string[] args)
{
// Start a task thread polling the databse for changes and updates the ASCII file
mainTask = new Task(PollDatabase, cts.Token, TaskCreationOptions.LongRunning);
mainTask.Start();
}
protected override void OnStop()
{
// Cancel Task and wait for it to be disposed
cts.Cancel();
mainTask.Wait();
}
我的变量
// Connection to database
private string connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
// Newest timestamp
private Int32 latestDate;
// Log file
public LogFileWriter logFileWriter;
// Define datastructure POCO class
EnvDataModel model = new EnvDataModel();
// Instantiate a concellation token for a task
private CancellationTokenSource cts = new CancellationTokenSource();
// Instantiate a task
private Task mainTask = null;
// Define a waiting interval between each database polling
private TimeSpan WaitAfterSuccessInterval = new TimeSpan(0, 1, 0);
// Define a waiting interval if any errors happens
private TimeSpan WaitAfterErrorInterval = new TimeSpan(0, 2, 0);
任务中的代码 运行
private void PollDatabase()
{
// New token
CancellationToken cancellation = cts.Token;
// Fencepost problem
// Setup default interval
TimeSpan interval = TimeSpan.Zero;
// Create a new logfile instance
logFileWriter = new LogFileWriter(@"C:\LogFile.txt");
// Poll database until service stops
while (!cancellation.WaitHandle.WaitOne(interval))
{
try
{
// Poll database here and assign reponse to EnvDataModel
// Update status in logfile
logFileWriter.WriteToLogFile("Database Last Polled: " + DateTime.Now + Environment.NewLine);
// Call ASCII File Parser
ParseDataToASCIIFileAsync();
// Occasionally check the cancelation state
if (cancellation.IsCancellationRequested)
{
break;
}
interval = WaitAfterSuccessInterval;
}
catch (Exception caught)
{
// Log exception
logFileWriter.WriteToLogFile(caught.Message);
// Wait a few minutes before trying again
interval = WaitAfterErrorInterval;
}
}
}
我通过以下步骤解决了我的问题:
- 在 Visual Studio 中,右键单击您的解决方案,然后单击
Clean Solution
- 再次右键单击您的解决方案,然后单击
Build Solution
- [非强制] 将
bin/debug
文件夹中的所有文件复制到要从中 运行 服务的目录。该服务需要在您的本地驱动器上。
- 使用 Visual Studio 开发人员命令提示符和
installutil
安装服务
在尝试启动我的 Windows 服务时,当状态栏快到 50% 时突然出现以下错误:
Error 1053: The service did not respond to the start or control request in timely fashion
当我检查事件查看器时,我看到以下错误:
Application: EnvMonService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception
Exception Info: System.NullReferenceException
Stack: at EnvMonService.EnvMonService..ctor() at EnvMonService.Program.Main()
我看不出错误的来源 - 这是我的代码:
主要()
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new EnvMonService()
};
ServiceBase.Run(ServicesToRun);
}
构造函数
public EnvMonService()
{
InitializeComponent();
}
OnStart 和 OnStop
protected override void OnStart(string[] args)
{
// Start a task thread polling the databse for changes and updates the ASCII file
mainTask = new Task(PollDatabase, cts.Token, TaskCreationOptions.LongRunning);
mainTask.Start();
}
protected override void OnStop()
{
// Cancel Task and wait for it to be disposed
cts.Cancel();
mainTask.Wait();
}
我的变量
// Connection to database
private string connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
// Newest timestamp
private Int32 latestDate;
// Log file
public LogFileWriter logFileWriter;
// Define datastructure POCO class
EnvDataModel model = new EnvDataModel();
// Instantiate a concellation token for a task
private CancellationTokenSource cts = new CancellationTokenSource();
// Instantiate a task
private Task mainTask = null;
// Define a waiting interval between each database polling
private TimeSpan WaitAfterSuccessInterval = new TimeSpan(0, 1, 0);
// Define a waiting interval if any errors happens
private TimeSpan WaitAfterErrorInterval = new TimeSpan(0, 2, 0);
任务中的代码 运行
private void PollDatabase()
{
// New token
CancellationToken cancellation = cts.Token;
// Fencepost problem
// Setup default interval
TimeSpan interval = TimeSpan.Zero;
// Create a new logfile instance
logFileWriter = new LogFileWriter(@"C:\LogFile.txt");
// Poll database until service stops
while (!cancellation.WaitHandle.WaitOne(interval))
{
try
{
// Poll database here and assign reponse to EnvDataModel
// Update status in logfile
logFileWriter.WriteToLogFile("Database Last Polled: " + DateTime.Now + Environment.NewLine);
// Call ASCII File Parser
ParseDataToASCIIFileAsync();
// Occasionally check the cancelation state
if (cancellation.IsCancellationRequested)
{
break;
}
interval = WaitAfterSuccessInterval;
}
catch (Exception caught)
{
// Log exception
logFileWriter.WriteToLogFile(caught.Message);
// Wait a few minutes before trying again
interval = WaitAfterErrorInterval;
}
}
}
我通过以下步骤解决了我的问题:
- 在 Visual Studio 中,右键单击您的解决方案,然后单击
Clean Solution
- 再次右键单击您的解决方案,然后单击
Build Solution
- [非强制] 将
bin/debug
文件夹中的所有文件复制到要从中 运行 服务的目录。该服务需要在您的本地驱动器上。 - 使用 Visual Studio 开发人员命令提示符和
installutil
安装服务