简体   繁体   中英

Why is my XDocument saving an incomplete file?

I have a program that runs a series of plugins using threads. During this process, it writes the runtimes of the plugins to an XDocument so that the program knows how long it's been since the last time that plugin was run. I'm having a problem, though. About once per day (at unpredictable times) when I load the XDocument I get the following error:

[04/01/2013 08:17:10.083] Unexpected end of file has occurred. The following elements are not closed: Database, DatabaseList. Line 4043, position 1.

I ran some trace statements and found out that the time before that, the service failed with about 44 plugins left to run, which apparently caused the XDocument to close without writing the end of the file. It writes to and reads from an XML file on the hard disk, but it performs all operations on the XDocument in memory because I use Linq to do complex operations on the data.

Does anybody know why this might happen? How can I load my XDocument so that it won't wreck the actual file if something happens during the running process?

EDIT: Here's a sample of the code that utilizes the XDocument (named XDoc ):

    private void RunPlugin(object oQueuedPlugin)
    {
        PluginState oPluginState = (PluginState)oQueuedPlugin;
        PluginResponse oResponse = new PluginResponse();
        XElement xPlugin;

        lock (xDoc)
        {
            xPlugin = GetPluginNode(oPluginState.ClientFusionDatabase.Name, oPluginState.Plugin.Name);
        }

        if (xPlugin == null)
        {
            API.Log.Write("ActivityTrace.ShowXMLLog", "XML for " + oPluginState.ClientFusionDatabase.Name + " was null.");
            XElement NewPlugin = new XElement("Plugin",
                new XAttribute("PluginName", oPluginState.Plugin.Name),
                new XAttribute("Running", "true"),
                new XAttribute("LastStart", DateTime.Now.ToString()),
                new XAttribute("LastSuccess", ""),
                new XAttribute("LastExitStatus",""));

            lock (xDoc)
            {
                var Location = from database in xDoc.Root.Elements("Database")
                               where database.Attribute("DatabaseName").Value == oPluginState.ClientFusionDatabase.Name
                               select database;

                Location.FirstOrDefault().Add(NewPlugin);
                xDoc.Save(XmlLogFilePath);
            }

            oResponse = oPluginState.Plugin.Run(oPluginState.ClientFusionDatabase);
            if (oResponse == null)
            {
                API.Log.Write("ActivityTrace.ShowNullReturnLog", oPluginState.ClientFusionDatabase.Name + "- " + oPluginState.Plugin.Name + " returned null.");
            }

            lock (xDoc)
            {
                NewPlugin.Attribute("Running").Value = "false";
                NewPlugin.Attribute("LastExitStatus").Value = oResponse.ResponseType.ToString();
                if (oResponse.ResponseType == PluginResponseTypes.Success || oResponse.ResponseType == PluginResponseTypes.Warning)
                    NewPlugin.Attribute("LastSuccess").Value = DateTime.Now.ToString();
                xDoc.Save(XmlLogFilePath);
            }

            API.Log.Write("ActivityTrace.ShowXMLLog","Completed " + oPluginState.ClientFusionDatabase.Name + "- " + oPluginState.Plugin.Name + " with XML " + NewPlugin.ToString());
            API.Log.Write(oPluginState.Plugin.Name, "(" + oPluginState.ClientFusionDatabase.Connection.Database + " = " + (oResponse.ResponseType + ") ").PadRight(9) + "EXIT MESSAGE: " + (string.IsNullOrEmpty(oResponse.Message) ? "None" : oResponse.Message));
        }
        else
        {
            DateTime dLastRun = (DateTime)xPlugin.Attribute("LastStart");
            bool bRunning = (bool)xPlugin.Attribute("Running");

            if ((DateTime.Now - dLastRun) > oPluginState.Plugin.Interval && !bRunning)
            {
                lock (xDoc)
                {
                    xPlugin.Attribute("LastStart").Value = DateTime.Now.ToString();
                    xPlugin.Attribute("Running").Value = "true";
                    xDoc.Save(XmlLogFilePath);
                }
                oResponse = oPluginState.Plugin.Run(oPluginState.ClientFusionDatabase);

                lock (xDoc)
                {
                    xPlugin.Attribute("Running").Value = "false";
                    xPlugin.Attribute("LastExitStatus").Value = oResponse.ResponseType.ToString();
                    if (oResponse.ResponseType == PluginResponseTypes.Success || oResponse.ResponseType == PluginResponseTypes.Warning)
                        xPlugin.Attribute("LastSuccess").Value = DateTime.Now.ToString();
                    xDoc.Save(XmlLogFilePath);
                }

                API.Log.Write(oPluginState.Plugin.Name, "(" + oPluginState.ClientFusionDatabase.Connection.Database + " = " + (oResponse.ResponseType + ") ").PadRight(9) + "EXIT MESSAGE: " + (string.IsNullOrEmpty(oResponse.Message) ? "None" : oResponse.Message));
            }
            else if (bRunning)
                API.Log.Write(oPluginState.Plugin.Name, "(" + oPluginState.ClientFusionDatabase.Connection.Database + " = " + ("SKIPPED) ").PadRight(9) + "REASON: Plugin already running");
        }

        oPluginState.Complete = true;
    }

The problem is that one or more of the plugins is not handling an error correctly, which causes it to not return any response and crash the program.

To read XML files without parsing the whole thing and loading it into memory, you can use the XmlReader class . Then to write XML files, you can use the XmlWriter class . There are some examples on the respective MSDN pages.

However, you'll lose all benefits of LINQ and it works quite a bit differently. There is really no way to combine the benefits of LINQ and XDocument while working on the XML files only on disk.

And then when your service crashes, your XmlWriter might still not get disposed, not flushing its buffers to disk and leaving you still with an incomplete XML file. You should solve the bug that causes the service to crash instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM