Intermittent errors while de-serializing object from XML

前端 未结 4 2187
面向向阳花
面向向阳花 2021-02-09 02:30

I have a program that takes objects stored as XML in a database (basicly a message queue) and de-serializes them. Intermittently, I will get one of the following errors:

<
4条回答
  •  北荒
    北荒 (楼主)
    2021-02-09 02:53

    For the first error (cannot execute a program), you might be running into the same XmlSerializer bug that we ran into. It turns out XmlSerlializer throws that exception when Directory.CurrentDirectory is set to a folder that no longer exists.

    Our specific situation is different than yours, but I'll give the details in case it helps shed light on what might be happening for you, or it helps anyone else. In our case, a small number of our customers would get that error after launching our WinForms application directly from the installer, i.e. they chose the "run now" option after installing or upgrading. (Unclear why it happened to some but not others). What we suspect is happening is that our installer (InstallAware) occasionally starts our application with the current directory set to a folder that no longer exists, or is about to be deleted. To test this theory, I wrote a test app which simulates launching from the installer:

        string dir = @"C:\Users\me\Documents\Temp\WillBeDeleted";
        Directory.CreateDirectory(dir);
        Directory.SetCurrentDirectory(dir);
    
        Process.Start(@"C:\Program Files (x86)\...\our.exe");
    
        Directory.SetCurrentDirectory(@"C:\");  // otherwise, won't be able to delete
        Directory.Delete(dir);
    

    Sure enough, as soon as the launched application created a new instance of XmlSerializer, the exception would be thrown. I put in trace statements to show the result of GetCurrentDirectory(), and indeed it was set to the WillBeDeleted folder. The fix was to SetCurrentDirectory to a valid location during application initialization, before any serialization took place.

提交回复
热议问题