0

I'm writing some vb.net code to retrieve some data from middleware, do some stuff with it, and then save a dictionary of name/value pairs to cache for quick retrieval when needed.

It all seems to go well until I retrieve the object from cache and it is always Null/Nothing.

Yet in the watch window I can see my cached object and it has data in it.

Thinking maybe it's a serialization issue I made a new class that inherits Dictionary and is marked as serializable but I still get the same results.

So right now I have this class:

<Serializable()> Public Class SerialDict
    Inherits Dictionary(Of String, String)

    Public Sub New()

    End Sub

End Class

I populate it and put it into cache like this:

 Dim Licenses As New SerialDict
                For Each r As DataRow In dtLicenses.Rows
                    Dim prikey As String = r("SettingID").ToString.Trim
                    Dim decryptionKey As String = GetHash((xx))

                    Dim licData As String = DecryptData(r("SettingVal"), decryptionKey)

                    Licenses.Add(r("SettingKey"), licData)
                Next

                If IsNothing(HttpContext.Current.Cache("Licenses")) Then
                    HttpContext.Current.Cache.Add("Licences", Licenses, Nothing, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Default, Nothing)
                End If

Then elsewhere we need to check that data so I try to retrieve it like this:

Dim Licences As SerialDict = CType(HttpContext.Current.Cache("Licenses"), SerialDict)

At this point Licenses is always Nothing, but the watch window shows data in HttpContext.Current.Cache("Licenses").

Any suggestions? Thanks!

MattB
  • 11
  • 4
  • A Dictionary already is serializable, so you don't need a new class for it. – Visual Vincent Apr 29 '16 at 19:39
  • And as far as I can see you never even serialize it. Perhaps serialize it to a base64-string before putting it in the cache? – Visual Vincent Apr 29 '16 at 20:01
  • Thanks for the response. I guess I don't understand this well enough to go from here. I thought if something was serializable (either declared as so or natively) that just made it able to be stored in session or cache. I'm not familiar with an explicit serialization for this use. Looking into how to serialize like you suggest now... – MattB Apr 29 '16 at 20:21
  • I don't know how the cache works, but it has nothing to do with the serialization. The serialization has to be done by you. Here's how you can perform binary serialization (easily convertable to VB.NET via an online converter): http://stackoverflow.com/a/2861749/3740093 – Visual Vincent Apr 29 '16 at 20:24
  • Also, you should probably use `DirectCast` instead of `CType`. – Visual Vincent Apr 29 '16 at 20:30

0 Answers0