.net TopShelf服务未启动
我有一个.NET控制台应用程序,使用TopShelf将此应用程序作为windows服务运行。控制台应用程序本身在运行和执行它应该执行的操作时没有问题。我已经安装了TopShelf nuget软件包,并且已经正确配置了TopShelf(据我所知) 当我调试应用程序时,或者甚至在我将应用程序安装到Windows服务并启动服务之后,它似乎没有执行“DoUpdate”方法,因为我已经定义了要在经过的事件中执行的计时器。但事实并非如此。有线索吗 以下是我在Console应用程序中的主要方法:.net TopShelf服务未启动,.net,windows-services,topshelf,.net,Windows Services,Topshelf,我有一个.NET控制台应用程序,使用TopShelf将此应用程序作为windows服务运行。控制台应用程序本身在运行和执行它应该执行的操作时没有问题。我已经安装了TopShelf nuget软件包,并且已经正确配置了TopShelf(据我所知) 当我调试应用程序时,或者甚至在我将应用程序安装到Windows服务并启动服务之后,它似乎没有执行“DoUpdate”方法,因为我已经定义了要在经过的事件中执行的计时器。但事实并非如此。有线索吗 以下是我在Console应用程序中的主要方法: pu
public static void Main()
{
try
{
HostFactory.Run(x =>
{
x.Service<DNSUpdateTopShelf>(a =>
{
a.ConstructUsing(n => new DNSUpdateTopShelf());
a.WhenStarted(ts => ts.Start());
a.WhenStopped(ts => ts.Stop());
});
x.SetDescription("Update GoDaddy DNS Records");
x.SetDisplayName("GoDaddy DNS Updater");
x.SetServiceName("GoDaddyDNSUpdate");
//x.RunAsNetworkService();
x.RunAsLocalSystem();
x.StartAutomatically();
});
}
catch (Exception e)
{
System.Console.WriteLine(e.Message);
}
System.Console.WriteLine("---------------");
System.Console.ReadKey();
}
publicstaticvoidmain()
{
尝试
{
HostFactory.Run(x=>
{
x、 服务(a=>
{
a、 构造使用(n=>newDNSupdateTopshelf());
a、 开始时(ts=>ts.Start());
a、 停止时(ts=>ts.Stop());
});
x、 SetDescription(“更新GoDaddy DNS记录”);
x、 SetDisplayName(“GoDaddy DNS更新程序”);
x、 SetServiceName(“GoDaddyDNSUpdate”);
//x、 RunAsNetworkService();
x、 RunAsLocalSystem();
x、 StartAutomatically();
});
}
捕获(例外e)
{
系统控制台写入线(e.Message);
}
System.Console.WriteLine(“--------------”;
System.Console.ReadKey();
}
下面是完成这项工作的班级:
public class DNSUpdateTopShelf
{
private System.Timers.Timer updateTimer;
private int interval;
private EventLog log;
public DNSUpdateTopShelf()
{
Initialize();
}
public void Initialize()
{
//log = new EventLog("DynaProLogs");
updateTimer = new System.Timers.Timer();
}
public void Start()
{
try
{
Utils.WriteLog("In Start Method");
var jsonFile = $"{Directory.GetCurrentDirectory()}\\appconfig.json";
using (StreamReader r = new StreamReader(jsonFile))
{
var json = r.ReadToEndAsync().Result;
var config = (JObject)JsonConvert.DeserializeObject(json);
interval = config["interval"].Value<int>();
}
updateTimer.Interval = interval;
updateTimer.Elapsed += new ElapsedEventHandler(DoUpdate);
}
catch (Exception ex)
{
Utils.WriteLog($"DNSUpdater - {ex.Message}", type: EventLogEntryType.Error);
throw;
}
}
public void Stop()
{
Utils.WriteLog("In Stop Method");
updateTimer.Stop();
}
public void DoUpdate(object sender, ElapsedEventArgs e)
{
Utils.WriteLog("In DoUpdate method");
DNSUpdater updater = new DNSUpdater();
var results = Task.Run( async () => await updater.UpdateAllDomainIPAsync()).Result;
//var results = updater.UpdateAllDomainIPAsync().Result;
foreach (UpdateResult result in results)
{
Utils.WriteLog($"{result.RequestURI} -- {result.Message}");
}
}
}
公共类DNSUpdateTopShelf
{
private System.Timers.Timer updateTimer;
私有整数间隔;
私有事件日志;
公共DNSUpdateTopShelf()
{
初始化();
}
公共无效初始化()
{
//日志=新事件日志(“DynaProLogs”);
updateTimer=new System.Timers.Timer();
}
公开作废开始()
{
尝试
{
Utils.WriteLog(“启动方法”);
var jsonFile=$“{Directory.GetCurrentDirectory()}\\appconfig.json”;
使用(StreamReader r=newstreamreader(jsonFile))
{
var json=r.ReadToEndAsync().Result;
var config=(JObject)JsonConvert.DeserializeObject(json);
interval=config[“interval”].Value();
}
updateTimer.Interval=Interval;
updateTimer.Appead+=新的ElapsedEventHandler(DoUpdate);
}
捕获(例外情况除外)
{
Utils.WriteLog($“DNSUpdater-{ex.Message}”,类型:EventLogEntryType.Error);
投掷;
}
}
公共停车场()
{
Utils.WriteLog(“停止方法”);
updateTimer.Stop();
}
public void DoUpdate(对象发送方,ElapsedEventArgs e)
{
Utils.WriteLog(“在双更新方法中”);
DNSUpdater updater=新的DNSUpdater();
var results=Task.Run(async()=>await updater.UpdateAllDomainIPAsync()).Result;
//var results=updater.UpdateAllDomainIPAsync().Result;
foreach(结果中的UpdateResult结果)
{
Utils.WriteLog($“{result.RequestURI}--{result.Message}”);
}
}
}
问题是:DoWork方法永远不会被调用。我已将计时器的ElapsedTime更改为许多不同的持续时间。但它似乎从来没有被呼叫过
我可以确认timer对象的“Interval”属性是否已设置。我还可以确认此服务已安装在Windows服务中并正在运行。正如我所说的,我试图通过调试整个应用程序(包括TopShelf)以及将其作为服务构建和安装之后对其进行测试。我不明白为什么计时器已过事件处理程序(DoUpdate)没有启动。在谷歌搜索答案后,我终于找到了答案。如果其他人也有同样的问题,下面是我所做的。我缺少计时器对象上的两个设置。在Start()方法中,我设置了计时器对象的AutoReset和Enabled属性。通过此修改,它现在将触发ElapsedTime事件,应用程序将按预期工作 这是我修改过的代码:
updateTimer.Elapsed += new ElapsedEventHandler(DoUpdate);
updateTimer.AutoReset = true;
updateTimer.Enabled = true;
您正在设置计时器的Interval属性并将一个事件分配给Appeased,但您从未启动计时器。这就是为什么我没有开火 呼叫或设置 正如Enabled的文档所述:
true
如果计时器应引发已过事件;否则,false
默认值为false
对于Start:
通过将Enabled设置为true,开始引发已用事件
还显示您必须启动计时器。仅提及自动重置默认为true。感谢您的回复。我在这里找了4天这样的建议,幸运的是,我坚持不懈的谷歌搜索终于有了回报。如果在你发布你的答案之前我没有找到完全相同的答案,我会将你的答案标记为已接受。不过,谢谢你的帮助。