我们有一个令人困惑的情况,正常运行数百次的代码突然停止工作。这是一个通常运行数周的应用程序。
问题是,XmlSerializer(Type)是否在某个地方有一些缓存,这些缓存可能会被破坏?
背景:
它发生在启动时,有一次,我们得到了很多例外。当检测到问题时(几天后)重新启动后,它再次正常运行。
我们已经追踪到这段代码的问题:
internal static class StateManager
{
private static XmlSerializer queueSerializer = new XmlSerializer(typeof(List<QueueItem>));
private static readonly string queuePath = Path.Combine(SubSystem.PersistentDirectory, "Queue.xml");
internal static void SaveQueue(List<QueueItem> upcomingTasks)
{
XmlWriter xmlWriter = XmlWriter.Create(queuePath, xmlSettings);
queueSerializer.Serialize(xmlWriter, upcomingTasks);
xmlWriter.Close();
}
internal static List<QueueItem> GetQueue()
{
var queue = new List<QueueItem>();
try
{
var xmlDoc = new XmlDocument();
xmlDoc.Load(queuePath);
using (XmlReader reader = XmlReader.Create(new StringReader(xmlDoc.OuterXml)))
{
queue = queueSerializer.Deserialize(reader) as List<QueueItem>;
}
}
catch (Exception e)
{
AppTrace.Write(TraceLevel.Error, string.Format("Failed to load State Queue: {0}", e.Message));
}
return queue;
}
}我们得到的错误是:
Failed to load State Queue: The type initializer for 'StateManager' threw an exception.据我们所知,这给罪魁祸首留下了两种可能性:
private static XmlSerializer queueSerializer = new XmlSerializer(typeof(List<QueueItem>));或
private static readonly string queuePath = Path.Combine(SubSystem.PersistentDirectory, "Queue.xml");我们仔细检查了SubSystem.PersistentDirectory,并相信它是清白的。
由于这种情况发生在客户端机器的现场,并且我们不能重现它,因此不可能检查内部异常。
发布于 2013-10-08 23:02:10
你应该明白这一点!我看到那里没有静态的ctor,你可以尝试这样做,推迟初始化,这样你就能知道更多:
internal static class StateManager
{
private static XmlSerializer queueSerializer;
private static readonly string queuePath;
internal static StateManager(){
try
{
queueSerializer = new XmlSerializer(typeof(List<QueueItem>));
queuePath = Path.Combine(SubSystem.PersistentDirectory, "Queue.xml");
}
catch(Exception ex)
{
// Log, log, log!
throw; // Essential: you MUST rethrow!
}
}
}至于实际的违规行,如果没有跟踪,就无法确定:您所知道的就是您的类型无法初始化,并且没有任何迹象表明原因。据我所知,最可能的原因是:
XmlSerializer的数据(不是XmlSerializer本身:我非常怀疑来自System命名空间的任何东西都容易随机爆炸)SubSystem.PersistentDirectory包含损坏的up,但您永远不会知道……)其他一些东西被破坏,异常实际上与违规代码无关,这些代码可能驻留在的其他地方
https://stackoverflow.com/questions/19251027
复制相似问题