How to save and retrieve Dictionary<object, HashSet<string>> in session

55 Views Asked by At

I have a class ImportResult with some properties:

public class ImportResult
{
    public string Logs { get; set; }
    public Dictionary<Person, HashSet<string>> InjuredPeople { get; set; }
}

Then in controller I want to save this InjuredPeople to the HttpContext.Session so I can use it somewhere else in the code:

ImportResult imporResults = someService.GetData();
HttpContext.Session.Set("InjuredPeople", importResults.InjuredPeople);

This is how I am also trying to get back this value from session in some different method:

public static Dictionary<Person, HashSet<string>> GetInjuredPeople()
{
    var content = HttpContext.Session.Get<Dictionary<Person, HashSet<string>>>("InjuredPeople");
    return content == null ? new Dictionary<Device, HashSet<string>>() : content;
}

And my Get method for session:

public static T? Get<T>(this ISession session, string key)
{
     var value = session.GetString(key);

     var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };

     return value == null ? default : JsonConvert.DeserializeObject<T>(value, settings);
}

I am getting error at return in Get<T> method:

Could not convert string '..Person' to dictionary key type '..Person'

How can I fix that?

0

There are 0 best solutions below