472,806 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,806 software developers and data experts.

BinaryFormatter.Deserialize

Hi.

While executing BinaryFormatter.Deserialize() I get:
System.InvalidCastException: Specified cast is not valid.
I implemented ISerializable interface. What may be a problem?

Thanks.
Nov 16 '05 #1
11 3880
Igor <no***@hotmail.com> wrote:
While executing BinaryFormatter.Deserialize() I get:
System.InvalidCastException: Specified cast is not valid.
I implemented ISerializable interface. What may be a problem?


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
[Serializable()]

public class ApplicationInfo:IDisposable,ISerializable

{

private Guid session; // 16 bytes

private Guid sessionNW; // 16 bytes

private string ANI;

private string DNIS;

private string mqNW; // networking queue

private string parent;

private string child;

private string applicationQueue;

private int machineID;

private int boardID;

private int channelID;

private string applicationName = "";

public ApplicationInfo()

{

}

public ApplicationInfo(SerializationInfo info, StreamingContext ctxt)

{

session = (Guid)info.GetValue("session", typeof(Guid));

sessionNW = (Guid)info.GetValue("sessionNW", typeof(Guid));

ANI = (string)info.GetValue("ANI", typeof(string));

DNIS = (string)info.GetValue("DNIS", typeof(string));

mqNW = (string)info.GetValue("mqNW", typeof(string));

parent = (string)info.GetValue("parent", typeof(string));

child = (string)info.GetValue("child", typeof(string));

applicationQueue = (string)info.GetValue("applicationQueue",
typeof(string));

applicationName = (string)info.GetValue("applicationName", typeof(string));

machineID = (int)info.GetValue("machineID", typeof(int));

boardID = (int)info.GetValue("boardID", typeof(int));

channelID = (int)info.GetValue("channelID", typeof(int));

}

public ApplicationInfo(string session, string sessionNW, string ANI, string
DNIS, string mqNW, string parent, string child, string applicationQueue, int
machine, int board, int channel, string applicationName)

{

this.session = new Guid(session);

this.sessionNW = new Guid(sessionNW);

this.ANI = ANI;

this.DNIS = DNIS;

this.mqNW = mqNW;

this.parent = parent;

this.child = child;

this.applicationQueue = applicationQueue;

this.machineID = machine;

this.boardID = board;

this.channelID = channel;

this.applicationName = applicationName;

}

#region IDisposable Members

public void Dispose()

{

// TODO: Add ApplicationInfo.Dispose implementation

}

#endregion

#region ISerializable Members

public void GetObjectData(SerializationInfo info, StreamingContext context)

{

info.AddValue("session", session);

info.AddValue("sessionNW", sessionNW);

info.AddValue("ANI", ANI);

info.AddValue("DNIS", DNIS);

info.AddValue("mqNW", mqNW);

info.AddValue("parent", parent);

info.AddValue("child", child);

info.AddValue("applicationQueue", applicationQueue);

info.AddValue("machineID", machineID);

info.AddValue("boardID", boardID);

info.AddValue("channelID", channelID);

info.AddValue("applicationName", applicationName);

}

#endregion

}

..........

ApplicationInfo app;

MemoryStream stream = new MemoryStream();

BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(stream, app);

MessageQueue mq = new MessageQueue(mqPath);

Message m = new Message(stream, new BinaryMessageFormatter());

mq.Formatter = new BinaryMessageFormatter();

mq.Send(m);

.........

public void OnReceiveCompleted(Object source, ReceiveCompletedEventArgs
asyncResult)

{

try

{

MessageQueue mq = (MessageQueue)source;

Message m = mq.EndReceive(asyncResult.AsyncResult);

m.Formatter = new BinaryMessageFormatter();

incomingMessage(source, new IncomingMessageEventArgs((object)m.BodyStream,
false));

mq.BeginReceive();

}

catch(Exception ex)

{

Console.WriteLine(ex.ToString());

}

}

......

public void OnIncomingMessage(object source, IncomingMessageEventArgs e)

{

BinaryFormatter formatter = new BinaryFormatter();

application = null;

application = (ApplicationInfo)formatter.Deserialize((Stream)e.g etDocument);

}



"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Igor <no***@hotmail.com> wrote:
While executing BinaryFormatter.Deserialize() I get:
System.InvalidCastException: Specified cast is not valid.
I implemented ISerializable interface. What may be a problem?


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
Igor <no***@hotmail.com> wrote:

<snip>

See http://www.pobox.com/~skeet/csharp/incomplete.html

It would help if you didn't post the code in such a way that there's a
blank line between each real line of code, too.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
[Serializable()]
public class ApplicationInfo:IDisposable,ISerializable
{
private Guid session; // 16 bytes
private Guid sessionNW; // 16 bytes
private string ANI;
private string DNIS;
private string mqNW; // networking queue
private string parent;
private string child;
private string applicationQueue;
private int machineID;
private int boardID;
private int channelID;
private string applicationName = "";
public ApplicationInfo()
{
}
public ApplicationInfo(SerializationInfo info, StreamingContext ctxt)
{
session = (Guid)info.GetValue("session", typeof(Guid));
sessionNW = (Guid)info.GetValue("sessionNW", typeof(Guid));
ANI = (string)info.GetValue("ANI", typeof(string));
DNIS = (string)info.GetValue("DNIS", typeof(string));
mqNW = (string)info.GetValue("mqNW", typeof(string));
parent = (string)info.GetValue("parent", typeof(string));
child = (string)info.GetValue("child", typeof(string));
applicationQueue = (string)info.GetValue("applicationQueue",typeof(st ring));
applicationName = (string)info.GetValue("applicationName", typeof(string));
machineID = (int)info.GetValue("machineID", typeof(int));
boardID = (int)info.GetValue("boardID", typeof(int));
channelID = (int)info.GetValue("channelID", typeof(int));
}

public ApplicationInfo(string session, string sessionNW, string ANI, string
DNIS, string mqNW, string parent, string child, string applicationQueue, int
machine, int board, int channel, string applicationName)
{
this.session = new Guid(session);
this.sessionNW = new Guid(sessionNW);
this.ANI = ANI;
this.DNIS = DNIS;
this.mqNW = mqNW;
this.parent = parent;
this.child = child;
this.applicationQueue = applicationQueue;
this.machineID = machine;
this.boardID = board;
this.channelID = channel;
this.applicationName = applicationName;
}

#region IDisposable Members
public void Dispose()
{
// TODO: Add ApplicationInfo.Dispose implementation
}
#endregion

#region ISerializable Members
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("session", session);
info.AddValue("sessionNW", sessionNW);
info.AddValue("ANI", ANI);
info.AddValue("DNIS", DNIS);
info.AddValue("mqNW", mqNW);
info.AddValue("parent", parent);
info.AddValue("child", child);
info.AddValue("applicationQueue", applicationQueue);
info.AddValue("machineID", machineID);
info.AddValue("boardID", boardID);
info.AddValue("channelID", channelID);
info.AddValue("applicationName", applicationName);
}
#endregion
}

..........

ApplicationInfo app;
MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, app);
MessageQueue mq = new MessageQueue(mqPath);
Message m = new Message(stream, new BinaryMessageFormatter());
mq.Formatter = new BinaryMessageFormatter();
mq.Send(m);

.........
public void OnReceiveCompleted(Object source, ReceiveCompletedEventArgs
asyncResult)
{
try
{
MessageQueue mq = (MessageQueue)source;
Message m = mq.EndReceive(asyncResult.AsyncResult);
m.Formatter = new BinaryMessageFormatter();
incomingMessage(source, new
IncomingMessageEventArgs((object)m.BodyStream,fals e));
mq.BeginReceive();
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}

......

public void OnIncomingMessage(object source, IncomingMessageEventArgs e)
{
BinaryFormatter formatter = new BinaryFormatter();
application = null;
application = (ApplicationInfo)formatter.Deserialize((Stream)e.g etDocument);
}



"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Igor <no***@hotmail.com> wrote:

<snip>

See http://www.pobox.com/~skeet/csharp/incomplete.html

It would help if you didn't post the code in such a way that there's a
blank line between each real line of code, too.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
Igor <no***@hotmail.com> wrote:

<snip>

See http://www.pobox.com/~skeet/csharp/incomplete.html again.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Igor,

While I haven't taken the time to run the code, I can see that there is
nothing in your class that requires custom serialization. All you have to
do is add the Serializable attribute, and your serialization should work
fine. ISerializable only needs to be implemented when there are other
things (besides the fields in your class) that you want to serialize, or
they require certain logic to be executed.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Igor" <no***@hotmail.com> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
[Serializable()]

public class ApplicationInfo:IDisposable,ISerializable

{

private Guid session; // 16 bytes

private Guid sessionNW; // 16 bytes

private string ANI;

private string DNIS;

private string mqNW; // networking queue

private string parent;

private string child;

private string applicationQueue;

private int machineID;

private int boardID;

private int channelID;

private string applicationName = "";

public ApplicationInfo()

{

}

public ApplicationInfo(SerializationInfo info, StreamingContext ctxt)

{

session = (Guid)info.GetValue("session", typeof(Guid));

sessionNW = (Guid)info.GetValue("sessionNW", typeof(Guid));

ANI = (string)info.GetValue("ANI", typeof(string));

DNIS = (string)info.GetValue("DNIS", typeof(string));

mqNW = (string)info.GetValue("mqNW", typeof(string));

parent = (string)info.GetValue("parent", typeof(string));

child = (string)info.GetValue("child", typeof(string));

applicationQueue = (string)info.GetValue("applicationQueue",
typeof(string));

applicationName = (string)info.GetValue("applicationName",
typeof(string));

machineID = (int)info.GetValue("machineID", typeof(int));

boardID = (int)info.GetValue("boardID", typeof(int));

channelID = (int)info.GetValue("channelID", typeof(int));

}

public ApplicationInfo(string session, string sessionNW, string ANI,
string
DNIS, string mqNW, string parent, string child, string applicationQueue,
int
machine, int board, int channel, string applicationName)

{

this.session = new Guid(session);

this.sessionNW = new Guid(sessionNW);

this.ANI = ANI;

this.DNIS = DNIS;

this.mqNW = mqNW;

this.parent = parent;

this.child = child;

this.applicationQueue = applicationQueue;

this.machineID = machine;

this.boardID = board;

this.channelID = channel;

this.applicationName = applicationName;

}

#region IDisposable Members

public void Dispose()

{

// TODO: Add ApplicationInfo.Dispose implementation

}

#endregion

#region ISerializable Members

public void GetObjectData(SerializationInfo info, StreamingContext
context)

{

info.AddValue("session", session);

info.AddValue("sessionNW", sessionNW);

info.AddValue("ANI", ANI);

info.AddValue("DNIS", DNIS);

info.AddValue("mqNW", mqNW);

info.AddValue("parent", parent);

info.AddValue("child", child);

info.AddValue("applicationQueue", applicationQueue);

info.AddValue("machineID", machineID);

info.AddValue("boardID", boardID);

info.AddValue("channelID", channelID);

info.AddValue("applicationName", applicationName);

}

#endregion

}

.........

ApplicationInfo app;

MemoryStream stream = new MemoryStream();

BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(stream, app);

MessageQueue mq = new MessageQueue(mqPath);

Message m = new Message(stream, new BinaryMessageFormatter());

mq.Formatter = new BinaryMessageFormatter();

mq.Send(m);

........

public void OnReceiveCompleted(Object source, ReceiveCompletedEventArgs
asyncResult)

{

try

{

MessageQueue mq = (MessageQueue)source;

Message m = mq.EndReceive(asyncResult.AsyncResult);

m.Formatter = new BinaryMessageFormatter();

incomingMessage(source, new IncomingMessageEventArgs((object)m.BodyStream,
false));

mq.BeginReceive();

}

catch(Exception ex)

{

Console.WriteLine(ex.ToString());

}

}

.....

public void OnIncomingMessage(object source, IncomingMessageEventArgs e)

{

BinaryFormatter formatter = new BinaryFormatter();

application = null;

application =
(ApplicationInfo)formatter.Deserialize((Stream)e.g etDocument);

}



"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Igor <no***@hotmail.com> wrote:
> While executing BinaryFormatter.Deserialize() I get:
> System.InvalidCastException: Specified cast is not valid.
> I implemented ISerializable interface. What may be a problem?


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #7
Earlier I tried only with [Serializable()] attribute, it didnt worked
either. :(
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Oc**************@TK2MSFTNGP15.phx.gbl...
Igor,

While I haven't taken the time to run the code, I can see that there is nothing in your class that requires custom serialization. All you have to
do is add the Serializable attribute, and your serialization should work
fine. ISerializable only needs to be implemented when there are other
things (besides the fields in your class) that you want to serialize, or
they require certain logic to be executed.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Igor" <no***@hotmail.com> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
[Serializable()]

public class ApplicationInfo:IDisposable,ISerializable

{

private Guid session; // 16 bytes

private Guid sessionNW; // 16 bytes

private string ANI;

private string DNIS;

private string mqNW; // networking queue

private string parent;

private string child;

private string applicationQueue;

private int machineID;

private int boardID;

private int channelID;

private string applicationName = "";

public ApplicationInfo()

{

}

public ApplicationInfo(SerializationInfo info, StreamingContext ctxt)

{

session = (Guid)info.GetValue("session", typeof(Guid));

sessionNW = (Guid)info.GetValue("sessionNW", typeof(Guid));

ANI = (string)info.GetValue("ANI", typeof(string));

DNIS = (string)info.GetValue("DNIS", typeof(string));

mqNW = (string)info.GetValue("mqNW", typeof(string));

parent = (string)info.GetValue("parent", typeof(string));

child = (string)info.GetValue("child", typeof(string));

applicationQueue = (string)info.GetValue("applicationQueue",
typeof(string));

applicationName = (string)info.GetValue("applicationName",
typeof(string));

machineID = (int)info.GetValue("machineID", typeof(int));

boardID = (int)info.GetValue("boardID", typeof(int));

channelID = (int)info.GetValue("channelID", typeof(int));

}

public ApplicationInfo(string session, string sessionNW, string ANI,
string
DNIS, string mqNW, string parent, string child, string applicationQueue,
int
machine, int board, int channel, string applicationName)

{

this.session = new Guid(session);

this.sessionNW = new Guid(sessionNW);

this.ANI = ANI;

this.DNIS = DNIS;

this.mqNW = mqNW;

this.parent = parent;

this.child = child;

this.applicationQueue = applicationQueue;

this.machineID = machine;

this.boardID = board;

this.channelID = channel;

this.applicationName = applicationName;

}

#region IDisposable Members

public void Dispose()

{

// TODO: Add ApplicationInfo.Dispose implementation

}

#endregion

#region ISerializable Members

public void GetObjectData(SerializationInfo info, StreamingContext
context)

{

info.AddValue("session", session);

info.AddValue("sessionNW", sessionNW);

info.AddValue("ANI", ANI);

info.AddValue("DNIS", DNIS);

info.AddValue("mqNW", mqNW);

info.AddValue("parent", parent);

info.AddValue("child", child);

info.AddValue("applicationQueue", applicationQueue);

info.AddValue("machineID", machineID);

info.AddValue("boardID", boardID);

info.AddValue("channelID", channelID);

info.AddValue("applicationName", applicationName);

}

#endregion

}

.........

ApplicationInfo app;

MemoryStream stream = new MemoryStream();

BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(stream, app);

MessageQueue mq = new MessageQueue(mqPath);

Message m = new Message(stream, new BinaryMessageFormatter());

mq.Formatter = new BinaryMessageFormatter();

mq.Send(m);

........

public void OnReceiveCompleted(Object source, ReceiveCompletedEventArgs
asyncResult)

{

try

{

MessageQueue mq = (MessageQueue)source;

Message m = mq.EndReceive(asyncResult.AsyncResult);

m.Formatter = new BinaryMessageFormatter();

incomingMessage(source, new IncomingMessageEventArgs((object)m.BodyStream, false));

mq.BeginReceive();

}

catch(Exception ex)

{

Console.WriteLine(ex.ToString());

}

}

.....

public void OnIncomingMessage(object source, IncomingMessageEventArgs e)

{

BinaryFormatter formatter = new BinaryFormatter();

application = null;

application =
(ApplicationInfo)formatter.Deserialize((Stream)e.g etDocument);

}



"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Igor <no***@hotmail.com> wrote:
> While executing BinaryFormatter.Deserialize() I get:
> System.InvalidCastException: Specified cast is not valid.
> I implemented ISerializable interface. What may be a problem?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



Nov 16 '05 #8
Igor,

What is the problem when you have JUST the serializable attribute, and
remove the serialization constructor and the implementation of
ISerializable?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Igor" <no***@hotmail.com> wrote in message
news:uo**************@TK2MSFTNGP12.phx.gbl...
Earlier I tried only with [Serializable()] attribute, it didnt worked
either. :(
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:Oc**************@TK2MSFTNGP15.phx.gbl...
Igor,

While I haven't taken the time to run the code, I can see that there

is
nothing in your class that requires custom serialization. All you have
to
do is add the Serializable attribute, and your serialization should work
fine. ISerializable only needs to be implemented when there are other
things (besides the fields in your class) that you want to serialize, or
they require certain logic to be executed.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Igor" <no***@hotmail.com> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
> [Serializable()]
>
> public class ApplicationInfo:IDisposable,ISerializable
>
> {
>
> private Guid session; // 16 bytes
>
> private Guid sessionNW; // 16 bytes
>
> private string ANI;
>
> private string DNIS;
>
> private string mqNW; // networking queue
>
> private string parent;
>
> private string child;
>
> private string applicationQueue;
>
> private int machineID;
>
> private int boardID;
>
> private int channelID;
>
> private string applicationName = "";
>
> public ApplicationInfo()
>
> {
>
> }
>
> public ApplicationInfo(SerializationInfo info, StreamingContext ctxt)
>
> {
>
> session = (Guid)info.GetValue("session", typeof(Guid));
>
> sessionNW = (Guid)info.GetValue("sessionNW", typeof(Guid));
>
> ANI = (string)info.GetValue("ANI", typeof(string));
>
> DNIS = (string)info.GetValue("DNIS", typeof(string));
>
> mqNW = (string)info.GetValue("mqNW", typeof(string));
>
> parent = (string)info.GetValue("parent", typeof(string));
>
> child = (string)info.GetValue("child", typeof(string));
>
> applicationQueue = (string)info.GetValue("applicationQueue",
> typeof(string));
>
> applicationName = (string)info.GetValue("applicationName",
> typeof(string));
>
> machineID = (int)info.GetValue("machineID", typeof(int));
>
> boardID = (int)info.GetValue("boardID", typeof(int));
>
> channelID = (int)info.GetValue("channelID", typeof(int));
>
> }
>
> public ApplicationInfo(string session, string sessionNW, string ANI,
> string
> DNIS, string mqNW, string parent, string child, string
> applicationQueue,
> int
> machine, int board, int channel, string applicationName)
>
> {
>
> this.session = new Guid(session);
>
> this.sessionNW = new Guid(sessionNW);
>
> this.ANI = ANI;
>
> this.DNIS = DNIS;
>
> this.mqNW = mqNW;
>
> this.parent = parent;
>
> this.child = child;
>
> this.applicationQueue = applicationQueue;
>
> this.machineID = machine;
>
> this.boardID = board;
>
> this.channelID = channel;
>
> this.applicationName = applicationName;
>
> }
>
> #region IDisposable Members
>
> public void Dispose()
>
> {
>
> // TODO: Add ApplicationInfo.Dispose implementation
>
> }
>
> #endregion
>
> #region ISerializable Members
>
> public void GetObjectData(SerializationInfo info, StreamingContext
> context)
>
> {
>
> info.AddValue("session", session);
>
> info.AddValue("sessionNW", sessionNW);
>
> info.AddValue("ANI", ANI);
>
> info.AddValue("DNIS", DNIS);
>
> info.AddValue("mqNW", mqNW);
>
> info.AddValue("parent", parent);
>
> info.AddValue("child", child);
>
> info.AddValue("applicationQueue", applicationQueue);
>
> info.AddValue("machineID", machineID);
>
> info.AddValue("boardID", boardID);
>
> info.AddValue("channelID", channelID);
>
> info.AddValue("applicationName", applicationName);
>
> }
>
> #endregion
>
> }
>
> .........
>
> ApplicationInfo app;
>
> MemoryStream stream = new MemoryStream();
>
> BinaryFormatter formatter = new BinaryFormatter();
>
> formatter.Serialize(stream, app);
>
>
>
> MessageQueue mq = new MessageQueue(mqPath);
>
> Message m = new Message(stream, new BinaryMessageFormatter());
>
> mq.Formatter = new BinaryMessageFormatter();
>
> mq.Send(m);
>
>
>
> ........
>
>
>
> public void OnReceiveCompleted(Object source, ReceiveCompletedEventArgs
> asyncResult)
>
> {
>
> try
>
> {
>
> MessageQueue mq = (MessageQueue)source;
>
> Message m = mq.EndReceive(asyncResult.AsyncResult);
>
>
>
> m.Formatter = new BinaryMessageFormatter();
>
> incomingMessage(source, new IncomingMessageEventArgs((object)m.BodyStream, > false));
>
> mq.BeginReceive();
>
> }
>
> catch(Exception ex)
>
> {
>
> Console.WriteLine(ex.ToString());
>
> }
>
> }
>
>
>
> .....
>
>
>
> public void OnIncomingMessage(object source, IncomingMessageEventArgs
> e)
>
> {
>
> BinaryFormatter formatter = new BinaryFormatter();
>
> application = null;
>
> application =
> (ApplicationInfo)formatter.Deserialize((Stream)e.g etDocument);
>
> }
>
>
>
>
>
>
>
>
>
> "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
> news:MP************************@msnews.microsoft.c om...
>> Igor <no***@hotmail.com> wrote:
>> > While executing BinaryFormatter.Deserialize() I get:
>> > System.InvalidCastException: Specified cast is not valid.
>> > I implemented ISerializable interface. What may be a problem?
>>
>> Could you post a short but complete program which demonstrates the
>> problem?
>>
>> See http://www.pobox.com/~skeet/csharp/complete.html for details of
>> what I mean by that.
>>
>> --
>> Jon Skeet - <sk***@pobox.com>
>> http://www.pobox.com/~skeet
>> If replying to the group, please do not mail me too
>
>



Nov 16 '05 #9
Thats what I did and I got the same exception
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2******************@tk2msftngp13.phx.gbl...
Igor,

What is the problem when you have JUST the serializable attribute, and
remove the serialization constructor and the implementation of
ISerializable?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Igor" <no***@hotmail.com> wrote in message
news:uo**************@TK2MSFTNGP12.phx.gbl...
Earlier I tried only with [Serializable()] attribute, it didnt worked
either. :(
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in
message news:Oc**************@TK2MSFTNGP15.phx.gbl...
Igor,

While I haven't taken the time to run the code, I can see that there
is
nothing in your class that requires custom serialization. All you have
to
do is add the Serializable attribute, and your serialization should

work fine. ISerializable only needs to be implemented when there are other
things (besides the fields in your class) that you want to serialize, or they require certain logic to be executed.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Igor" <no***@hotmail.com> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
> [Serializable()]
>
> public class ApplicationInfo:IDisposable,ISerializable
>
> {
>
> private Guid session; // 16 bytes
>
> private Guid sessionNW; // 16 bytes
>
> private string ANI;
>
> private string DNIS;
>
> private string mqNW; // networking queue
>
> private string parent;
>
> private string child;
>
> private string applicationQueue;
>
> private int machineID;
>
> private int boardID;
>
> private int channelID;
>
> private string applicationName = "";
>
> public ApplicationInfo()
>
> {
>
> }
>
> public ApplicationInfo(SerializationInfo info, StreamingContext ctxt)
>
> {
>
> session = (Guid)info.GetValue("session", typeof(Guid));
>
> sessionNW = (Guid)info.GetValue("sessionNW", typeof(Guid));
>
> ANI = (string)info.GetValue("ANI", typeof(string));
>
> DNIS = (string)info.GetValue("DNIS", typeof(string));
>
> mqNW = (string)info.GetValue("mqNW", typeof(string));
>
> parent = (string)info.GetValue("parent", typeof(string));
>
> child = (string)info.GetValue("child", typeof(string));
>
> applicationQueue = (string)info.GetValue("applicationQueue",
> typeof(string));
>
> applicationName = (string)info.GetValue("applicationName",
> typeof(string));
>
> machineID = (int)info.GetValue("machineID", typeof(int));
>
> boardID = (int)info.GetValue("boardID", typeof(int));
>
> channelID = (int)info.GetValue("channelID", typeof(int));
>
> }
>
> public ApplicationInfo(string session, string sessionNW, string ANI,
> string
> DNIS, string mqNW, string parent, string child, string
> applicationQueue,
> int
> machine, int board, int channel, string applicationName)
>
> {
>
> this.session = new Guid(session);
>
> this.sessionNW = new Guid(sessionNW);
>
> this.ANI = ANI;
>
> this.DNIS = DNIS;
>
> this.mqNW = mqNW;
>
> this.parent = parent;
>
> this.child = child;
>
> this.applicationQueue = applicationQueue;
>
> this.machineID = machine;
>
> this.boardID = board;
>
> this.channelID = channel;
>
> this.applicationName = applicationName;
>
> }
>
> #region IDisposable Members
>
> public void Dispose()
>
> {
>
> // TODO: Add ApplicationInfo.Dispose implementation
>
> }
>
> #endregion
>
> #region ISerializable Members
>
> public void GetObjectData(SerializationInfo info, StreamingContext
> context)
>
> {
>
> info.AddValue("session", session);
>
> info.AddValue("sessionNW", sessionNW);
>
> info.AddValue("ANI", ANI);
>
> info.AddValue("DNIS", DNIS);
>
> info.AddValue("mqNW", mqNW);
>
> info.AddValue("parent", parent);
>
> info.AddValue("child", child);
>
> info.AddValue("applicationQueue", applicationQueue);
>
> info.AddValue("machineID", machineID);
>
> info.AddValue("boardID", boardID);
>
> info.AddValue("channelID", channelID);
>
> info.AddValue("applicationName", applicationName);
>
> }
>
> #endregion
>
> }
>
> .........
>
> ApplicationInfo app;
>
> MemoryStream stream = new MemoryStream();
>
> BinaryFormatter formatter = new BinaryFormatter();
>
> formatter.Serialize(stream, app);
>
>
>
> MessageQueue mq = new MessageQueue(mqPath);
>
> Message m = new Message(stream, new BinaryMessageFormatter());
>
> mq.Formatter = new BinaryMessageFormatter();
>
> mq.Send(m);
>
>
>
> ........
>
>
>
> public void OnReceiveCompleted(Object source, ReceiveCompletedEventArgs > asyncResult)
>
> {
>
> try
>
> {
>
> MessageQueue mq = (MessageQueue)source;
>
> Message m = mq.EndReceive(asyncResult.AsyncResult);
>
>
>
> m.Formatter = new BinaryMessageFormatter();
>
> incomingMessage(source, new

IncomingMessageEventArgs((object)m.BodyStream,
> false));
>
> mq.BeginReceive();
>
> }
>
> catch(Exception ex)
>
> {
>
> Console.WriteLine(ex.ToString());
>
> }
>
> }
>
>
>
> .....
>
>
>
> public void OnIncomingMessage(object source, IncomingMessageEventArgs
> e)
>
> {
>
> BinaryFormatter formatter = new BinaryFormatter();
>
> application = null;
>
> application =
> (ApplicationInfo)formatter.Deserialize((Stream)e.g etDocument);
>
> }
>
>
>
>
>
>
>
>
>
> "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
> news:MP************************@msnews.microsoft.c om...
>> Igor <no***@hotmail.com> wrote:
>> > While executing BinaryFormatter.Deserialize() I get:
>> > System.InvalidCastException: Specified cast is not valid.
>> > I implemented ISerializable interface. What may be a problem?
>>
>> Could you post a short but complete program which demonstrates the
>> problem?
>>
>> See http://www.pobox.com/~skeet/csharp/complete.html for details of
>> what I mean by that.
>>
>> --
>> Jon Skeet - <sk***@pobox.com>
>> http://www.pobox.com/~skeet
>> If replying to the group, please do not mail me too
>
>



Nov 16 '05 #10
application = (ApplicationInfo)formatter.Deserialize((Stream)e.g etDocument);

What tells splitting the above compound statement into a sequence of simple
statements you? What part gives the error? What are the actual types
involved? Make sure you indeed get back what you expect, show us the
splitted version and the real types involved. Sofar I see nothing wrong but
you have an error that pops up in the above line so there must be some
mismatch...

Nov 16 '05 #11
object obj = formatter.Deserialize((MemoryStream)e.getDocument) ;

ApplicationInfo .ApplicationInfo application = (ApplicationInfo.ApplicationInfo)obj;

I get that exception in second row.

In both applications, sender and receiver class defined:
namespace ApplicationInfo

{

[Serializable()]

public class ApplicationInfo:IDisposable

{

...

}
}
"Joep" <St***@DeStoep.nl> wrote in message news:41*********************@news.xs4all.nl...
application = (ApplicationInfo)formatter.Deserialize((Stream)e.g etDocument);

What tells splitting the above compound statement into a sequence of simple
statements you? What part gives the error? What are the actual types
involved? Make sure you indeed get back what you expect, show us the
splitted version and the real types involved. Sofar I see nothing wrong but
you have an error that pops up in the above line so there must be some
mismatch...


Nov 16 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dominic | last post by:
Hi everybody, In my application, I'm planning to use BinaryFormatter to serialize a potentially huge object to file (and, of course, deserialize back to memory later). My question is if there is...
5
by: aladdinm1 | last post by:
Hi All, I have an annoying trouble with binary serialization. I have a windows forms application which works like a server and keeps sending data to its clients. The data is serialized before...
0
by: aladdinm1 | last post by:
Hi All, Reference to the problem I posted with subject "BinaryFormatter.Deserialize fails when used with .net ActiveX". I could successfully solve the problem by creating a class inherited from...
3
by: Joshua Moore | last post by:
I have a webservice that serializes a ton of variables and other good stuff to a txt file using SoapFormatter (IFormatter), and when I try to deserialize it using the binary formatter, i get the...
0
by: Fred Heida | last post by:
Hi Al, i have a funny problem.. i you can call it funny.. what i have is 2 assemblies, the first one does nothing other then Application.Run(new MyForm())
0
by: Fruber Malcome | last post by:
I'm getting a very weird exception and hoping someone may be able to help. I have an Office Add-In that lives in a .dll (for email reference ai.dll) ai.dll makes calls into the core part of the...
19
by: Sharon | last post by:
Hi, When I'm doing BinaryFormatter.Deserialize() over a TCP socket. When I'm closing the TcpListener by invoking the TcpListener.Stop(); I get: System.IO.IOException with message "Unable to...
2
by: Doug Lind | last post by:
Hi all, I have seen a number of posts re: the BinaryFormatter version incompatibility but nothing on how to recover from it. In my case, I want the exception to trigger an alternate behaviour...
11
by: JZ | last post by:
Hi, I'm using a class and binary formatter to store data in files. For example.. Dim FPs As New StuctureDataFile() Dim FileStream As Stream = File.Open(pfile, FileMode.Open) Dim...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.