通常,我收到此错误:(本地计算机上的"服务名称"服务启动然后停止.如果我的代码出现问题,如某些服务未被其他服务或程序使用,则会自动停止驱动路径等.Windows服务无法启动.
如果达到大小限制,我有一个备份文件夹/文件的Windows服务到一个位置.详细信息都是由Windows服务在启动时读取的XML配置提供的.我有一个单独的窗体,有一个按钮,完全符合我的Windows服务的启动.在将其放入Windows服务之前,我使用我的Windows窗体来调试代码.
当我启动我的Windows窗体.它做了它想做的事情.当我把我的代码放在Windows服务OnStart()方法时出现错误.
这是我的代码:
protected override void OnStart(string[] args)
{
private static string backupConfig = @"D:\LogBackupConfig\backupconfig.xml";
private static string serviceStat = @"D:\LogBackupConfig\Status.txt";
private static string fileFolderStat = @"D:\LogBackupConfig\FileFolderStat.txt";
protected override void OnStart(string[] args)
{
if (File.Exists(backupConfig))
{
FileSystemWatcher watcher = new FileSystemWatcher();
XmlTextReader reader = new XmlTextReader(backupConfig);
XmlNodeType type;
List<string> listFile = new List<string>();
string fileWatch = "";
//this loop is for reading XML elements and assigning to variables
while (reader.Read())
{
type = reader.NodeType;
if (type == XmlNodeType.Element)
{ …Run Code Online (Sandbox Code Playgroud) 我创建了一个名为ProxyMonitor的Windows服务,我目前处于安装和卸载服务的阶段,就像我想要的那样.
所以我像这样执行应用程序:
C:\\Windows\\Vendor\\ProxyMonitor.exe /install
Run Code Online (Sandbox Code Playgroud)
非常自我解释,然后我得到services.msc并开始服务,但当我这样做时,我收到以下消息:
本地计算机上的代理监视器服务已启动,然后停止.如果没有工作要做,某些服务会自动停止,例如,性能日志和警报服务
我的代码看起来像这样:
public static Main(string[] Args)
{
if (System.Environment.UserInteractive)
{
/*
* Here I have my install logic
*/
}
else
{
ServiceBase.Run(new ProxyMonitor());
}
}
Run Code Online (Sandbox Code Playgroud)
然后在ProxyMonitor类中我有:
public ProxyMonitor()
{
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
ProxyEventLog.WriteEntry("ProxyMonitor Started");
running = true;
while (running)
{
//Execution Loop
}
}
Run Code Online (Sandbox Code Playgroud)
而onStop()我只是改变running变量false;
我需要做些什么来使服务保持活跃,因为我需要监控我需要跟踪变化等的网络.
更新:1
protected override void OnStart(string[] args)
{
base.OnStart(args);
ProxyEventLog.WriteEntry("ProxyMonitor Started");
Thread = new …Run Code Online (Sandbox Code Playgroud) 使用旧版 .NET,您可以通过Microsoft.AspNet.WebApi.OwinSelfHost NuGet 包自行托管 OWIN。
有没有办法在 .NET Core 控制台应用程序中自行托管 OWIN?