I'm using the StateServer mode to save information about the session. And we're using the ILookup<long, long> type, which is not serializable. So it gives an error that the type is not marked with that annotation [Serializable].
I found this question, which helps in the serialization so that we can store the information in the session without error. But now I can't deserialize the information so I can use it as a Lookup.
Below are the code samples.
Basically a method that fetches information about benefits id and returns an ILookup<long, long>
public ILookup<long, long> GetInconsistencies(long orderId)
{
var order = _orderRepository.Query().FirstOrDefault(x => x.Id == idPedido);
var unities = unitOrderRepository.Query().Where(x => x.Order.Id == orderUd && x.Unit.Delete).Select(x => x.Unit.Id).ToList();
var benefits = BenefitRepository.Query().Where(x => x.OrderId == orderId && x.Delete).Select(x => x.Id);
var xUnities = unities.ToLookup(x => (long)EntityEnum.Unity, x => x);
var xBenefits = benefits.ToLookup(x => (long)EntityEnum.Benefit, x => x);
return xUnities.Concat(xBenefits)
.SelectMany(lookup => lookup.Select(value => new { lookup.Key, value }))
.Distinct()
.ToLookup(x => x.Key, x => x.value);
}
At first, we would try to save this information like this:
Session.Add(SESSAO_EXCLUSAO, GetInconsistencies(orderId));
But given the error that ILookup is not marked as serializable and given the question in the link I put above, we did it like this:
Session.Add(SESSAO_EXCLUSAO, JsonConvert.SerializeObject(GetInconsistencies(orderId), new LookupSerializer()));
And when we try to deserialize the information as an ILookup<long, long> we are not succeeding:
At this point of deserializing, the value of Session[SESSAO_EXCLUSAO] is this:
"{\"3\":[51008988]}"
And JsonConvert.DeserializeObject(Session[SESSAO_EXCLUSAO].ToString()) returns this:
{{
"3": [
51008988
]
}}
But when we put it all together:
JsonConvert.DeserializeObject(Session[SESSAO_EXCLUSAO].ToString()) as ILookup<long, long>
It returns null.
I have no ideia of what else we could be doing here to fix this. Can you help me please? Thanks in advance.