Azure Redis Caching 的序列化



我有一个托管在 Azure 应用服务上的 ASP.Net 应用。

我知道默认的">InProc"会话缓存在 Azure 中不起作用。

正在寻找解决方法,并遇到了 Redis,我已经在 Azure 中启用了它并在配置文件中设置了以下内容

<sessionState mode="Custom" customProvider="RedisStateStore">
<providers>
<add name="RedisStateStore" type="Microsoft.Web.Redis.RedisSessionStateProvider" 
host="xxx.redis.cache.windows.net:6380" accessKey="xxx=" ssl="true"
redisSerializerType="MyApp.Net.Helper.JsonSerializer,MyApp.Net"
/>
</providers>
</sessionState></system.web>

默认情况下,我开始收到某些表单的程序集未序列化错误。由于我将 EF 与 50 个表一起使用,因此不确定要序列化哪个表,因此变得乏味。

因此在这里启用了自定义处理程序处理程序:

public class JsonSerializer : Microsoft.Web.Redis.ISerializer
{
private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore, PreserveReferencesHandling = PreserveReferencesHandling.Objects,
Error = (serializer, err) => {
err.ErrorContext.Handled = true;
}
};
public byte[] Serialize(object data)
{
return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data, Settings));
}
public object Deserialize(byte[] data)
{
return data == null ? null : JsonConvert.DeserializeObject(Encoding.UTF8.GetString(data), Settings);
}
}

但这就像序列化那里的每个对象,我觉得它实际上进一步减慢了系统的速度。然后它在某些对象数组上失败了,例如以下

如果我忽略像上面的代码这样的错误(我知道这是不好的做法),有时我会遇到System.OutOfMemoryException异常。

任何建议在这里表示感谢。

我开始收到某些表单的程序集未序列化错误

使用 Redis 的会话状态提供程序时,应确保存储在会话状态中的数据类型是可序列化的。

据我所知,错误消息应指示程序集未标记为可序列化的类型,这应该可以帮助我们快速找到特定类型不可从代码隐藏中序列化。

这就像序列化那里的每个对象,我觉得它实际上进一步减慢了系统的速度。然后它在某些对象数组上失败了

您可以根据在网页中看到的错误详细信息Type 'xxx' in Assembly 'xxx, Version=...' is not marked as serializable找到特定的类型/对象,并尝试仅对这些类型/对象进行序列化。此外,如果可能的话,你可以对对象的某些字段使用NonSerializedAttribute。

最新更新