我正在使用Quartz.net,并尝试让Quartz服务器在Windows服务中启动。我创建了一个Windows服务项目并包含了Quartz.net库。在我的Service类中,我有:
protected override void OnStart(string[] args)
{
try
{
Host host = HostFactory.New(x =>
{
x.Service<IQuartzServer>(s =>
{
s.SetServiceName("quartz.server");
s.ConstructUsing(builder =>
{
QuartzServer server = new QuartzServer();
server.Initialize();
return server;
});
s.WhenStarted(server => server.Start());
s.WhenPaused(server => server.Pause());
s.WhenContinued(server => server.Resume());
s.WhenStopped(server => server.Stop());
});
x.RunAsLocalService();
//x.RunAs(@"mydomain\mysusername", "mypassword");
x.SetDescription(Configuration.ServiceDescription);
x.SetDisplayName(Configuration.ServiceDisplayName);
x.SetServiceName(Configuration.ServiceName);
});
host.Run();
}
catch (Exception ex)
{
Log.Error(ex.Message);
Log.Error(ex.InnerException.Message);
}
}
我还创建了一个Windows服务安装程序,并使用以下命令在Visual Studio的命令提示符中成功安装了Windows服务:
installutil MyWindowsService.exe
当我在Windows服务列表中查看我的服务并尝试启动服务时,会弹出消息对话框:
The MyWindowsService service on Local Computer started and the
stopped. Some Services stop automatically if they are not in use by
other services or programs.
这里是我记录在事件查看器(log4net)中的输出:
Windows 事件
1
Information 05/12/2012 14:52 MyWindowsService.exe "2012-12-05
14:52:24,044 [11528] INFO
Common.Logging.Factory.AbstractLogger.Info(:0) - Finished Starting
MyProject Windows Service."
2
Error 05/12/2012 14:52 Service1 "Service cannot be started.
System.NullReferenceException: Object reference not set to an instance
of an object. at MyWindowsService.MyProject.OnStart(String[] args)
in c:\My Projects\MyProject
v40\CO40\MyWindowsService\MyProject.cs:line 58 at
System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object
state)"
3
Error 05/12/2012 14:52 MyWindowsService.exe "2012-12-05 14:52:24,042
[6048] ERROR Common.Logging.Factory.AbstractLogger.Error(:0) - The
Topshelf.HostConfigurators.WindowsServiceDescription service has not
been installed yet. Please run 'MyWindowsService, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null install'. "
4
Error 05/12/2012 14:52 MyWindowsService.exe "2012-12-05 14:52:24,041
[6048] FATAL Topshelf.Windows.WindowsServiceHost.Run(:0) - The
Topshelf.HostConfigurators.WindowsServiceDescription service has not
been installed yet. Please run 'MyWindowsService, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null install'. "
5
Information 05/12/2012 14:52 MyWindowsService.exe "2012-12-05
14:52:24,039 [6048] INFO Topshelf.Windows.WindowsServiceHost.Run(:0)
- Starting up as a winservice application "
6
Information 05/12/2012 14:52 MyWindowsService.exe "2012-12-05
14:52:24,038 [6048] DEBUG Topshelf.Builders.RunBuilder.CreateHost(:0)
- Running as a Windows service, using the service host "
7
Information 05/12/2012 14:52 MyWindowsService.exe "2012-12-05
14:52:24,027 [6048] INFO Topshelf.OS.OsDetector.DetectOs(:0) -
Detected the operating system: 'win' "
8
Information 05/12/2012 14:52 MyWindowsService.exe "2012-12-05
14:52:23,895 [6048] INFO
Topshelf.HostConfigurators.HostConfiguratorImpl.CreateHost(:0) -
Topshelf v2.2.2.0, .NET Framework v4.0.30319.17929 "
9
Information 05/12/2012 14:52 MyWindowsService.exe "2012-12-05
14:52:23,829 [11528] INFO
Common.Logging.Factory.AbstractLogger.Info(:0) - Starting MyProject
Windows Service.. "
有人知道我怎样才能让这个服务在不抛出错误的情况下启动吗?
提前感谢。
ex.InnerException.Message
很容易导致空指针。请避免。 - mugume david