我通过TopShelf创建了一个Windows服务。但是,当服务在Start()期间遇到错误时,它总是显示一个对话框"错误5:访问被拒绝"。
我想知道是否有办法在我的代码中自定义错误....
TopshelfExitCode exitCode = HostFactory.Run(x =>
{
x.BeforeInstall(ApplyConfigTransform);
x.AddCommandLineDefinition(DataServiceHost.ANONYMIZE_NAMES, s => options.Add(DataServiceHost.ANONYMIZE_NAMES, s));
x.AddCommandLineDefinition(DataServiceHost.PORT_OVERRIDE, s => options.Add(DataServiceHost.PORT_OVERRIDE, s));
x.Service<DataServiceHost>(s =>
{
s.ConstructUsing(name => new DataServiceHost());
s.WhenStarted(host => host.Start(options));
s.WhenStopped(host => host.Stop());
s.WhenShutdown(host => host.LogEvent("Shutdown"));
s.WhenPaused(host => host.LogEvent("Paused"));
s.WhenContinued(host => host.LogEvent("Continued"));
s.WhenSessionChanged((host, scArgs) => host.LogSessionChanged(scArgs));
});
x.SetServiceName("service name");
x.SetDisplayName("service name");
x.SetDescription("service desc");
x.EnableServiceRecovery(rc =>
{
rc.RestartService(1); // restart the service after 1 minute
rc.SetResetPeriod(1); // set the reset interval to one day
});
x.RunAsLocalSystem();
});