Solved

C# ->> Memcache and deserialisation

Posted on 2014-03-24
8
317 Views
Last Modified: 2014-03-24
Hi,

I have a little C# application that successfully collects a value from memcache, it then attemptss to deserialise what I have using a memoryStream and BinaryFormatter.

Here is the code...

using (MemcachedClient writeClient = new MemcachedClient())
            {                
                string frObj= writeClient.Get<string>("myser");                
                byte[] byteArray = Encoding.ASCII.GetBytes(frObj);
                MemoryStream stream = new MemoryStream(byteArray);
                stream.Position = 0;
                BinaryFormatter bf = new BinaryFormatter();
                object d = bf.Deserialize(stream);
            }

Open in new window


I can get at the data and retrieve it. I just cant deserialise it into an object. The follow line gives me a SerialisationException was unhandled runtime.
 object d = bf.Deserialize(stream);

Open in new window


It states the following.
End of Stream encountered before parsing was completed.

I was wondering if anyone can help me to resolve this and prevent this runtime error or at least handle it.

Thank you.
Regards,
0
Comment
Question by:meetsys
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39951214
Deserialize the byte array that the MemoryStream holds, not the MemoryStream itself. In other words, change line 8 to:

 object d = bf.Deserialize(stream.ToArray());

Open in new window

0
 

Author Comment

by:meetsys
ID: 39951237
That just gives off more runtime errors.

I get this one...

cannot convert from 'byte[]' to 'System.IO.Stream'

and an invalid arguments. This site is great. I never know about it before. Any ideas where I am going wrong.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39951241
What does your serialization code look like?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:meetsys
ID: 39951251
The serialisation code is a string of data that comes from another program. In fact it is actually serialised in .php and then passed to the 3rd party memcache and then picked up in c#. I'm just trying to deserialise it, not worried if it comes out wrong. Even if its not the same as how it went in. Just want to have something output.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39951258
If we're talking about the same PHP code that you demonstrated in your previous question, then I don't see where there is any serialization going on. Even if there were, you are talking about serialization/deserialization between two different programming languages. If you truly are serializing in your PHP code, then you need to use a form of serialization that is more cross-platform, like XML or JSON.
0
 

Author Comment

by:meetsys
ID: 39951272
Sure I get it. But it should still do something. Some sort of process that doesn't involve the runtime error I was enquiring about. Thats all I am looking to resolve at present.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39951277
OK, then if I am correct in that you are not serializing in the PHP, then simply remove the BinaryFormatter business.

i.e.

using (MemcachedClient writeClient = new MemcachedClient())
{                
    string frObj = writeClient.Get<string>("myser");
}

Open in new window


Since it's a string that you put into memcached, that should be what you are getting back. No need to deserialize anything. Had you put something more involved into memcached--say a JSON object--then deserailization may come into play.
0
 

Author Closing Comment

by:meetsys
ID: 39951482
Thanks for all your advise kaufmed. Really appreciate it. Tricky business, some of this serialisation, memcache stuff. Thanks
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Adjust the codes 3 63
Data Analysis 7 59
Health check of winows and webservces 5 51
MCAT Verbal Reasoning 2 21
A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question