JT1979
JT1979

Reputation: 181

StackExchange RedisValue ToByteArray not Serializable

I am very new to StackExchange.Redis but I have been assigned a task to convert our Redis Cache to StackExchange.Redis. That said in one of our methods return the database values as a byte[].

when I try: var redisData = database.StringGet(key).ToByteArray();

I get this error:

Type 'StackExchange.Redis.RedisValue' in Assembly 'StackExchange.Redis, Version=1.1.608.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable

but when I do this: var redisData = database.StringGet(key); I get a value.

so my quesstion is: how can i convert redisData to a byte[]?

Upvotes: 2

Views: 1688

Answers (1)

kiziu
kiziu

Reputation: 1130

If you take a look at the type that is returned by StringGet(), which is RedisValue, you will see, that is it implicitly convertible to string and byte[]. All you need to do to convert it is to simply use it as variable of type byte[] (let the implicit operator do the work) or cast it explicitly.

var redisData = (byte[])database.StringGet(key);

I assumed, that data you stored was a proper byte[] array. Otherwise, if you stored a string, eg. serialized object, you will get the byte representation of string.

Upvotes: 4

Related Questions