Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调用XDocument.Root.Add(XElement)时服务冻结_C#_Linq To Xml - Fatal编程技术网

C# 调用XDocument.Root.Add(XElement)时服务冻结

C# 调用XDocument.Root.Add(XElement)时服务冻结,c#,linq-to-xml,C#,Linq To Xml,我正在开发一项服务,它定期扫描某个文件夹,以收集其中某些文件及其子文件夹的信息 我已经在一个控制台应用程序中测试了代码,它按预期运行,但是当它从我的服务运行时,执行似乎停留在调用scanLog.Root.Add(temp)的地方 我可以看到写着“开始添加节点”的事件条目,但是它永远不会到达“完成添加节点”的位置。有没有想过为什么这在我的服务中不起作用,但在控制台应用程序中却可以正常工作 private void ScanForChildCopies(string dir) {

我正在开发一项服务,它定期扫描某个文件夹,以收集其中某些文件及其子文件夹的信息

我已经在一个控制台应用程序中测试了代码,它按预期运行,但是当它从我的服务运行时,执行似乎停留在调用scanLog.Root.Add(temp)的地方

我可以看到写着“开始添加节点”的事件条目,但是它永远不会到达“完成添加节点”的位置。有没有想过为什么这在我的服务中不起作用,但在控制台应用程序中却可以正常工作

private void ScanForChildCopies(string dir)
    {
        foreach(var dirs in Directory.GetDirectories(dir))
        {
            ScanForChildCopies(dirs);
        }

        foreach(var files in Directory.GetFiles(dir, "*.ac"))
        {
            FileInfo fInfo = new FileInfo(files);
            serviceLogging.WriteEntry("Found File: " + files);
            if (fInfo.Exists)
            {
                if ((DateTime.Now - fInfo.LastWriteTime).Days > 0)
                {
                    serviceLogging.WriteEntry("Building node");
                    XElement temp = new XElement("childfile", new XElement("name", fInfo.Name), new XElement("lastmodified", fInfo.LastWriteTime.ToShortDateString()),
                        new XElement("age", (DateTime.Now - fInfo.LastWriteTime).Days.ToString()));
                    serviceLogging.WriteEntry("Starting to add node");
                    scanLog.Root.Add(temp);
                    serviceLogging.WriteEntry("finished Adding node to scanlog");                 
                }                    
            }
        }



    }

它从未执行的原因之一可能是您试图编辑与正在调用的服务位于同一文件夹中的文件。

我发现了导致服务挂起的问题

scanLog.Root.Add(temp);

scanLog在开始时被声明为静态XDocument,但在调用posted函数的函数中被重新声明。

在尝试向现有XDocument添加XElement(temp)时,它会挂起,因此在此阶段不会写入实际文件,它应该只是在内存中构建xml并将其添加到XDocument对象。当你说服务时,你是指IIS中托管的web服务还是指windows服务…@Umair Farooq我知道,如果你将任何内容写入映射文件夹,IIS将重置应用程序池。。。我只是说这可能发生在Vladmere案例中……一个windows服务,它是使用InstallUtil安装到我的本地机器上进行测试的。