Bla*_*tor 96 c# windows-services
通常,我收到此错误:(本地计算机上的"服务名称"服务启动然后停止.如果我的代码出现问题,如某些服务未被其他服务或程序使用,则会自动停止驱动路径等.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)
{
if (reader.Name == "File")
{
reader.Read();
fileWatch = reader.Value;
}
else if (reader.Name == "Folder")
{
reader.Read();
fileWatch = reader.Value;
}
}
}
reader.Close();
watcher.Path = fileWatch;
watcher.Filter = "*.*";
//this loop reads whether the service will watch a file/folder
XmlTextReader reader1 = new XmlTextReader(backupConfig);
while (reader1.Read())
{
type = reader1.NodeType;
if (type == XmlNodeType.Element)
{
if (reader1.Name == "File")
{
watcher.IncludeSubdirectories = false;
watcher.Changed += new FileSystemEventHandler(OnChangedFile);
}
else if (reader1.Name == "Folder")
{
watcher.IncludeSubdirectories = true;
watcher.Changed += new FileSystemEventHandler(OnChangedFolder);
}
}
}
reader1.Close();
watcher.EnableRaisingEvents = true;
}
else
{
StreamWriter sw = new StreamWriter(serviceStat, true);
sw.WriteLine("File not found. Please start the Log Backup UI first.");
sw.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道是什么让Windows服务无法启动,Windows窗体模拟器工作正常.什么似乎是问题?
更新:经过多次试验,我注意到只使用文件夹目录(w/out文件),Windows服务不起作用.当我用特定文件(包括其目录)替换fileWatch变量时,Windows服务启动了.当我将其更改回文件夹位置时,它无法正常工作.我认为文件夹位置在filewatcher中不起作用.
当我尝试创建一个监视文件夹位置的新Windows服务时,它工作..但是,当我在原始的Windows服务中尝试相同的位置时,它不起作用!我很精神!#ed ed!似乎每次我放置一个新的代码/函数时我都必须创建一个新的Windows服务并构建安装程序.这样我就可以跟踪我收到错误的位置.
McG*_*gle 188
如果服务像这样开始和停止,则意味着您的代码抛出了未处理的异常.这很难调试,但有一些选择.
Eya*_*l H 37
不确定这会有所帮助,但是对于调试服务,您总是可以在OnStart方法中使用以下内容:
protected override void OnStart(string[] args)
{
System.Diagnostics.Debugger.Launch();
...
}
Run Code Online (Sandbox Code Playgroud)
比你可以将你的视觉工作室附加到过程并具有更好的调试能力.
希望这有帮助,祝你好运
我发现通过简单地用以下方法更改程序,将现有的Windows服务转换为控制台非常方便.通过此更改,您可以通过在visual studio中调试或正常运行可执行文件来运行该程序.但它也可以作为Windows服务.我还发了一篇关于它的博客文章
Program.cs中
class Program
{
static void Main()
{
var program = new YOUR_PROGRAM();
if (Environment.UserInteractive)
{
program.Start();
}
else
{
ServiceBase.Run(new ServiceBase[]
{
program
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
YOUR_PROGRAM.cs
[RunInstallerAttribute(true)]
public class YOUR_PROGRAM : ServiceBase
{
public YOUR_PROGRAM()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
Start();
}
protected override void OnStop()
{
//Stop Logic Here
}
public void Start()
{
//Start Logic here
}
}
Run Code Online (Sandbox Code Playgroud)