I am working on a very old, but very large and very active ASP.NET webforms site, that uses a custom Session State provider that relies on JSON.Net.
One side effect of this is that objects that are based on plain, non-generic IEnumerable can be put in Session, but not retrieved from it. This is a JSON.Net limitation (an exception will be thrown if you try.)
Stripped of its details, my situation is this: I need to stash an object in Session, of a third-party-provided, sealed class, for which I don't have the source code, that's based on IEnumerable. (We'll call it VendorClassA.)
And: I need to be able to get it out again.
And that's what doesn't work.
I have tried mapping an instance of VendorClassA to something else, stashing that in Session, and then converting it back. That works quite well.
But it turns out that there's one place -- within the third-party, inaccessible code -- where an instance of VendorClassA is retrieved from Session, and if it can't be retrieved, or if the retrieved object can't be cast to VendorClassA, a fatal exception is thrown.
VendorClassA value = (VendorClassA) HttpContext.Current.Session["key"];
I can't change that code (third-party, no source code, etc.)
And I can't modify the code that does the actual JSON.Net serialization, to change which methods are invoked, or how, because that's deep in the custom session state provider. Which I have no access to, both programmatically and organizationally.
So, my question:
Is there any way I can affect how JSON.Net handles this specific class's serialization, given that I can't change any of the following:
- That the object I put into Session State needs to be an instance of
VendorClassA, or at least castable to it, because code I can't change will try to cast it toVendorClassA. - The attributes on
VendorClassA - That it's based on plain, non-generic
IEnumerable. - That
VendorClassAis sealed - How the JSON.Net serialization routines are called and what parameters or overloads they use.
Perhaps there's some way to introduce a custom contract for VendorClassA, that would be globally applicable, but which could be introduced from a distance?
Is there a better way?