ICSharpCode.SharpZipLib未能加载文件或程序集
Memcached使用的是0.84版本的dll,vs2013带的是0.86版本
在web.config中<runtime><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">节加入以下节点
<dependentAssembly>
<assemblyIdentity name="ICSharpCode.SharpZipLib" publicKeyToken="1B03E6ACF1164F73" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-0.86.0.518" newVersion="0.86.0.518" />
</dependentAssembly>
未标记为可序列化。
为所有要缓存的对象加可序列化属性太麻烦,需要对进行再次封装
/// <summary>
/// 向缓存服务器写入数据
/// </summary>
/// <param name="key">key</param>
/// <param name="value">数据</param>
/// <param name="seconds">缓存时间 </param>
/// <returns></returns>
public static bool SetData(string key, object value, int seconds)
{
string jsonString = JsonConvert.SerializeObject(value);
return MemcacheHelper.Set(key, jsonString, DateTime.Now.AddSeconds(seconds));
} /// <summary>
/// 从服务器读取数据
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="key">key</param>
/// <returns></returns>
public static T GetData<T>(string key)
{
object obj = MemcacheHelper.Get(key);
if (obj == null)
return default(T);
return JsonConvert.DeserializeObject<T>(obj.ToString());
}
使用上面的方法可能会导致复杂的集合类无法反序列化成功,报Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type .....错误
尝试将写入方法改为
/// <summary>
/// 向缓存服务器写入数据
/// </summary>
/// <param name="key">key</param>
/// <param name="value">数据</param>
/// <param name="seconds">缓存时间 </param>
/// <returns></returns>
public static bool SetData<T>(string key, T value, int seconds)
{
if (value == null)
return false;
string jsonString = JsonConvert.SerializeObject(value);
return MemcacheHelper.Set(key, jsonString, DateTime.Now.AddSeconds(seconds));
}
反序列化失败是偶尔出现,以待后测