473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 3915
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.