Fix a issue when deserialize binary storage.

This commit is contained in:
qianlifeng
2015-01-05 18:18:29 +08:00
parent 6162904c59
commit 714d8c7092
2 changed files with 33 additions and 5 deletions

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization.Formatters.Binary;
using System.Text; using System.Text;
using Wox.Infrastructure.Logger; using Wox.Infrastructure.Logger;
@@ -23,6 +24,8 @@ namespace Wox.Infrastructure.Storage
protected override void LoadInternal() protected override void LoadInternal()
{ {
//http://stackoverflow.com/questions/2120055/binaryformatter-deserialize-gives-serializationexception
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
try try
{ {
using (FileStream fileStream = new FileStream(ConfigPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (FileStream fileStream = new FileStream(ConfigPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
@@ -43,7 +46,32 @@ namespace Wox.Infrastructure.Storage
catch (Exception) catch (Exception)
{ {
serializedObject = LoadDefault(); serializedObject = LoadDefault();
#if (DEBUG)
{
throw;
}
#endif
} }
finally
{
AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
}
}
private System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
Assembly ayResult = null;
string sShortAssemblyName = args.Name.Split(',')[0];
Assembly[] ayAssemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly ayAssembly in ayAssemblies)
{
if (sShortAssemblyName == ayAssembly.FullName.Split(',')[0])
{
ayResult = ayAssembly;
break;
}
}
return ayResult;
} }
protected override void SaveInternal() protected override void SaveInternal()
@@ -58,11 +86,11 @@ namespace Wox.Infrastructure.Storage
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.Message); Log.Error(e.Message);
#if (DEBUG) #if (DEBUG)
{ {
throw e; throw;
} }
#endif #endif
} }
} }
} }

View File

@@ -2,7 +2,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries> <ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Default.xaml"></ResourceDictionary> <ResourceDictionary Source="Base.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries> </ResourceDictionary.MergedDictionaries>
<Style x:Key="QueryBoxStyle" BasedOn="{StaticResource BaseQueryBoxStyle}" TargetType="{x:Type TextBox}"> <Style x:Key="QueryBoxStyle" BasedOn="{StaticResource BaseQueryBoxStyle}" TargetType="{x:Type TextBox}">