This project has moved and is read-only. For the latest updates, please go here.

TargetInvocationException while serializing...

Oct 31, 2009 at 12:27 AM

Hello Jason, very nice work there...

I am using your serializer into a project, everything was smooth until i decided to serialize a List<State> one of my

custom classes instead another one i used to, cause i needed the info into this one....and boom i got this exception.

I serialize them converting into State[] to be sure everything goes ok in the CF. I use the CF and the DotNet libraries.

Tried to find something out but nothing came up after my efforts, so here i am...

The InnerException: "Timeouts are not supported on this stream."

The Method:


        private void ShowClients(State stateClient)
            var requestedClients =
                    .Where(client => client.ClientInfo.ClientIp != stateClient.ClientInfo.ClientIp);

            byte[] clientsList = Serialize.JsonObjectToByteArray(requestedClients.ToArray());
            SendData(clientsList, CommandInOrder.None, _zeroParameter, stateClient.TypeOfTheObject, stateClient);


The Serialize Static Method (Where the exception thrown too):


    internal class Serialize
        public static byte[] JsonObjectToByteArray(object obj)
            lock (obj)
   Exception--> string jsonString = JsonConvert.SerializeObject(obj);
                return Encoding.UTF8.GetBytes(jsonString);



The State class:


    public class State
        public Socket Socket { get; set; }

        private const int BufferSize = 1024;

        public byte[] ByteBuffer = new byte[BufferSize];

        public int NumBytes { get; set; }

        public MemoryStream MemStream = new MemoryStream();

        public ClientInfo ClientInfo = new ClientInfo();

        public CommandInOrder Command { get; set; }

        public string Parameter { get; set; }
        public string TypeOfTheObject { get; set; }


I used to send over the Socket, i use socket to transfer the objects, the ClientInfo class, but then needed the other properties too.

The ClientInfo class:


    public class ClientInfo
        public string ClientIp { get; set; }

        public string ClientName { get; set; }

        public int ClientId { get; set; }


If i take it back and use only the ClientInfo class, then my project works again, cant figure out what the problem is, serializer dont like something,

but is not telling me so i can fix...

Would appreciate any help to get this running again!

Nov 2, 2009 at 8:25 AM

The error is being thrown when accessing a property on your Stream object. I've improved the error message which gets thrown to make this clearer.

In any case serializing a Stream doesn't make any sense. A Stream is an abstraction over data. You want to serialize the data (a byte array).

Nov 2, 2009 at 8:30 AM

Dear James,

Thanx for the reply, i thought it would be an error on this property and i think i applied trying JsonIgnore but nothing happened!

Shouldn't the serializer ignore the property? Or i should use the DataContract attribute whe i haven't tried yet!



Nov 2, 2009 at 8:33 AM

Ignoring MemStream should have fixed that particular error, unless of course their are other Streams that I haven't noticed.

Nov 2, 2009 at 8:34 AM

I will try it again, thank you!