473,789 Members | 2,547 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BinaryFormatter .Deserialize

Hi.

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

Thanks.
Nov 16 '05 #1
11 3959
Igor <no***@hotmail. com> wrote:
While executing BinaryFormatter .Deserialize() I get:
System.InvalidC astException: 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.co m>
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,IS erializable

{

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 applicationQueu e;

private int machineID;

private int boardID;

private int channelID;

private string applicationName = "";

public ApplicationInfo ()

{

}

public ApplicationInfo (SerializationI nfo info, StreamingContex t ctxt)

{

session = (Guid)info.GetV alue("session", typeof(Guid));

sessionNW = (Guid)info.GetV alue("sessionNW ", typeof(Guid));

ANI = (string)info.Ge tValue("ANI", typeof(string)) ;

DNIS = (string)info.Ge tValue("DNIS", typeof(string)) ;

mqNW = (string)info.Ge tValue("mqNW", typeof(string)) ;

parent = (string)info.Ge tValue("parent" , typeof(string)) ;

child = (string)info.Ge tValue("child", typeof(string)) ;

applicationQueu e = (string)info.Ge tValue("applica tionQueue",
typeof(string)) ;

applicationName = (string)info.Ge tValue("applica tionName", typeof(string)) ;

machineID = (int)info.GetVa lue("machineID" , typeof(int));

boardID = (int)info.GetVa lue("boardID", typeof(int));

channelID = (int)info.GetVa lue("channelID" , typeof(int));

}

public ApplicationInfo (string session, string sessionNW, string ANI, string
DNIS, string mqNW, string parent, string child, string applicationQueu e, 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.applicatio nQueue = applicationQueu e;

this.machineID = machine;

this.boardID = board;

this.channelID = channel;

this.applicatio nName = applicationName ;

}

#region IDisposable Members

public void Dispose()

{

// TODO: Add ApplicationInfo .Dispose implementation

}

#endregion

#region ISerializable Members

public void GetObjectData(S erializationInf o info, StreamingContex t 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(" applicationQueu e", applicationQueu e);

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.Seria lize(stream, app);

MessageQueue mq = new MessageQueue(mq Path);

Message m = new Message(stream, new BinaryMessageFo rmatter());

mq.Formatter = new BinaryMessageFo rmatter();

mq.Send(m);

.........

public void OnReceiveComple ted(Object source, ReceiveComplete dEventArgs
asyncResult)

{

try

{

MessageQueue mq = (MessageQueue)s ource;

Message m = mq.EndReceive(a syncResult.Asyn cResult);

m.Formatter = new BinaryMessageFo rmatter();

incomingMessage (source, new IncomingMessage EventArgs((obje ct)m.BodyStream ,
false));

mq.BeginReceive ();

}

catch(Exception ex)

{

Console.WriteLi ne(ex.ToString( ));

}

}

......

public void OnIncomingMessa ge(object source, IncomingMessage EventArgs e)

{

BinaryFormatter formatter = new BinaryFormatter ();

application = null;

application = (ApplicationInf o)formatter.Des erialize((Strea m)e.getDocument );

}



"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Igor <no***@hotmail. com> wrote:
While executing BinaryFormatter .Deserialize() I get:
System.InvalidC astException: 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.co m>
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.co m>
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,IS erializable
{
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 applicationQueu e;
private int machineID;
private int boardID;
private int channelID;
private string applicationName = "";
public ApplicationInfo ()
{
}
public ApplicationInfo (SerializationI nfo info, StreamingContex t ctxt)
{
session = (Guid)info.GetV alue("session", typeof(Guid));
sessionNW = (Guid)info.GetV alue("sessionNW ", typeof(Guid));
ANI = (string)info.Ge tValue("ANI", typeof(string)) ;
DNIS = (string)info.Ge tValue("DNIS", typeof(string)) ;
mqNW = (string)info.Ge tValue("mqNW", typeof(string)) ;
parent = (string)info.Ge tValue("parent" , typeof(string)) ;
child = (string)info.Ge tValue("child", typeof(string)) ;
applicationQueu e = (string)info.Ge tValue("applica tionQueue",type of(string));
applicationName = (string)info.Ge tValue("applica tionName", typeof(string)) ;
machineID = (int)info.GetVa lue("machineID" , typeof(int));
boardID = (int)info.GetVa lue("boardID", typeof(int));
channelID = (int)info.GetVa lue("channelID" , typeof(int));
}

public ApplicationInfo (string session, string sessionNW, string ANI, string
DNIS, string mqNW, string parent, string child, string applicationQueu e, 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.applicatio nQueue = applicationQueu e;
this.machineID = machine;
this.boardID = board;
this.channelID = channel;
this.applicatio nName = applicationName ;
}

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

#region ISerializable Members
public void GetObjectData(S erializationInf o info, StreamingContex t 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(" applicationQueu e", applicationQueu e);
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.Seria lize(stream, app);
MessageQueue mq = new MessageQueue(mq Path);
Message m = new Message(stream, new BinaryMessageFo rmatter());
mq.Formatter = new BinaryMessageFo rmatter();
mq.Send(m);

.........
public void OnReceiveComple ted(Object source, ReceiveComplete dEventArgs
asyncResult)
{
try
{
MessageQueue mq = (MessageQueue)s ource;
Message m = mq.EndReceive(a syncResult.Asyn cResult);
m.Formatter = new BinaryMessageFo rmatter();
incomingMessage (source, new
IncomingMessage EventArgs((obje ct)m.BodyStream ,false));
mq.BeginReceive ();
}
catch(Exception ex)
{
Console.WriteLi ne(ex.ToString( ));
}
}

......

public void OnIncomingMessa ge(object source, IncomingMessage EventArgs e)
{
BinaryFormatter formatter = new BinaryFormatter ();
application = null;
application = (ApplicationInf o)formatter.Des erialize((Strea m)e.getDocument );
}



"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.co m>
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.co m>
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.co m

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

public class ApplicationInfo :IDisposable,IS erializable

{

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 applicationQueu e;

private int machineID;

private int boardID;

private int channelID;

private string applicationName = "";

public ApplicationInfo ()

{

}

public ApplicationInfo (SerializationI nfo info, StreamingContex t ctxt)

{

session = (Guid)info.GetV alue("session", typeof(Guid));

sessionNW = (Guid)info.GetV alue("sessionNW ", typeof(Guid));

ANI = (string)info.Ge tValue("ANI", typeof(string)) ;

DNIS = (string)info.Ge tValue("DNIS", typeof(string)) ;

mqNW = (string)info.Ge tValue("mqNW", typeof(string)) ;

parent = (string)info.Ge tValue("parent" , typeof(string)) ;

child = (string)info.Ge tValue("child", typeof(string)) ;

applicationQueu e = (string)info.Ge tValue("applica tionQueue",
typeof(string)) ;

applicationName = (string)info.Ge tValue("applica tionName",
typeof(string)) ;

machineID = (int)info.GetVa lue("machineID" , typeof(int));

boardID = (int)info.GetVa lue("boardID", typeof(int));

channelID = (int)info.GetVa lue("channelID" , typeof(int));

}

public ApplicationInfo (string session, string sessionNW, string ANI,
string
DNIS, string mqNW, string parent, string child, string applicationQueu e,
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.applicatio nQueue = applicationQueu e;

this.machineID = machine;

this.boardID = board;

this.channelID = channel;

this.applicatio nName = applicationName ;

}

#region IDisposable Members

public void Dispose()

{

// TODO: Add ApplicationInfo .Dispose implementation

}

#endregion

#region ISerializable Members

public void GetObjectData(S erializationInf o info, StreamingContex t
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(" applicationQueu e", applicationQueu e);

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.Seria lize(stream, app);

MessageQueue mq = new MessageQueue(mq Path);

Message m = new Message(stream, new BinaryMessageFo rmatter());

mq.Formatter = new BinaryMessageFo rmatter();

mq.Send(m);

........

public void OnReceiveComple ted(Object source, ReceiveComplete dEventArgs
asyncResult)

{

try

{

MessageQueue mq = (MessageQueue)s ource;

Message m = mq.EndReceive(a syncResult.Asyn cResult);

m.Formatter = new BinaryMessageFo rmatter();

incomingMessage (source, new IncomingMessage EventArgs((obje ct)m.BodyStream ,
false));

mq.BeginReceive ();

}

catch(Exception ex)

{

Console.WriteLi ne(ex.ToString( ));

}

}

.....

public void OnIncomingMessa ge(object source, IncomingMessage EventArgs e)

{

BinaryFormatter formatter = new BinaryFormatter ();

application = null;

application =
(ApplicationInf o)formatter.Des erialize((Strea m)e.getDocument );

}



"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Igor <no***@hotmail. com> wrote:
> While executing BinaryFormatter .Deserialize() I get:
> System.InvalidC astException: 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.co m>
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.c om> wrote in
message news:Oc******** ******@TK2MSFTN GP15.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.co m

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

public class ApplicationInfo :IDisposable,IS erializable

{

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 applicationQueu e;

private int machineID;

private int boardID;

private int channelID;

private string applicationName = "";

public ApplicationInfo ()

{

}

public ApplicationInfo (SerializationI nfo info, StreamingContex t ctxt)

{

session = (Guid)info.GetV alue("session", typeof(Guid));

sessionNW = (Guid)info.GetV alue("sessionNW ", typeof(Guid));

ANI = (string)info.Ge tValue("ANI", typeof(string)) ;

DNIS = (string)info.Ge tValue("DNIS", typeof(string)) ;

mqNW = (string)info.Ge tValue("mqNW", typeof(string)) ;

parent = (string)info.Ge tValue("parent" , typeof(string)) ;

child = (string)info.Ge tValue("child", typeof(string)) ;

applicationQueu e = (string)info.Ge tValue("applica tionQueue",
typeof(string)) ;

applicationName = (string)info.Ge tValue("applica tionName",
typeof(string)) ;

machineID = (int)info.GetVa lue("machineID" , typeof(int));

boardID = (int)info.GetVa lue("boardID", typeof(int));

channelID = (int)info.GetVa lue("channelID" , typeof(int));

}

public ApplicationInfo (string session, string sessionNW, string ANI,
string
DNIS, string mqNW, string parent, string child, string applicationQueu e,
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.applicatio nQueue = applicationQueu e;

this.machineID = machine;

this.boardID = board;

this.channelID = channel;

this.applicatio nName = applicationName ;

}

#region IDisposable Members

public void Dispose()

{

// TODO: Add ApplicationInfo .Dispose implementation

}

#endregion

#region ISerializable Members

public void GetObjectData(S erializationInf o info, StreamingContex t
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(" applicationQueu e", applicationQueu e);

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.Seria lize(stream, app);

MessageQueue mq = new MessageQueue(mq Path);

Message m = new Message(stream, new BinaryMessageFo rmatter());

mq.Formatter = new BinaryMessageFo rmatter();

mq.Send(m);

........

public void OnReceiveComple ted(Object source, ReceiveComplete dEventArgs
asyncResult)

{

try

{

MessageQueue mq = (MessageQueue)s ource;

Message m = mq.EndReceive(a syncResult.Asyn cResult);

m.Formatter = new BinaryMessageFo rmatter();

incomingMessage (source, new IncomingMessage EventArgs((obje ct)m.BodyStream , false));

mq.BeginReceive ();

}

catch(Exception ex)

{

Console.WriteLi ne(ex.ToString( ));

}

}

.....

public void OnIncomingMessa ge(object source, IncomingMessage EventArgs e)

{

BinaryFormatter formatter = new BinaryFormatter ();

application = null;

application =
(ApplicationInf o)formatter.Des erialize((Strea m)e.getDocument );

}



"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Igor <no***@hotmail. com> wrote:
> While executing BinaryFormatter .Deserialize() I get:
> System.InvalidC astException: 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.co m>
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.co m

"Igor" <no***@hotmail. com> wrote in message
news:uo******** ******@TK2MSFTN GP12.phx.gbl...
Earlier I tried only with [Serializable()] attribute, it didnt worked
either. :(
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in
message news:Oc******** ******@TK2MSFTN GP15.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.co m

"Igor" <no***@hotmail. com> wrote in message
news:OJ******** ******@TK2MSFTN GP10.phx.gbl...
> [Serializable()]
>
> public class ApplicationInfo :IDisposable,IS erializable
>
> {
>
> 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 applicationQueu e;
>
> private int machineID;
>
> private int boardID;
>
> private int channelID;
>
> private string applicationName = "";
>
> public ApplicationInfo ()
>
> {
>
> }
>
> public ApplicationInfo (SerializationI nfo info, StreamingContex t ctxt)
>
> {
>
> session = (Guid)info.GetV alue("session", typeof(Guid));
>
> sessionNW = (Guid)info.GetV alue("sessionNW ", typeof(Guid));
>
> ANI = (string)info.Ge tValue("ANI", typeof(string)) ;
>
> DNIS = (string)info.Ge tValue("DNIS", typeof(string)) ;
>
> mqNW = (string)info.Ge tValue("mqNW", typeof(string)) ;
>
> parent = (string)info.Ge tValue("parent" , typeof(string)) ;
>
> child = (string)info.Ge tValue("child", typeof(string)) ;
>
> applicationQueu e = (string)info.Ge tValue("applica tionQueue",
> typeof(string)) ;
>
> applicationName = (string)info.Ge tValue("applica tionName",
> typeof(string)) ;
>
> machineID = (int)info.GetVa lue("machineID" , typeof(int));
>
> boardID = (int)info.GetVa lue("boardID", typeof(int));
>
> channelID = (int)info.GetVa lue("channelID" , typeof(int));
>
> }
>
> public ApplicationInfo (string session, string sessionNW, string ANI,
> string
> DNIS, string mqNW, string parent, string child, string
> applicationQueu e,
> 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.applicatio nQueue = applicationQueu e;
>
> this.machineID = machine;
>
> this.boardID = board;
>
> this.channelID = channel;
>
> this.applicatio nName = applicationName ;
>
> }
>
> #region IDisposable Members
>
> public void Dispose()
>
> {
>
> // TODO: Add ApplicationInfo .Dispose implementation
>
> }
>
> #endregion
>
> #region ISerializable Members
>
> public void GetObjectData(S erializationInf o info, StreamingContex t
> 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(" applicationQueu e", applicationQueu e);
>
> 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.Seria lize(stream, app);
>
>
>
> MessageQueue mq = new MessageQueue(mq Path);
>
> Message m = new Message(stream, new BinaryMessageFo rmatter());
>
> mq.Formatter = new BinaryMessageFo rmatter();
>
> mq.Send(m);
>
>
>
> ........
>
>
>
> public void OnReceiveComple ted(Object source, ReceiveComplete dEventArgs
> asyncResult)
>
> {
>
> try
>
> {
>
> MessageQueue mq = (MessageQueue)s ource;
>
> Message m = mq.EndReceive(a syncResult.Asyn cResult);
>
>
>
> m.Formatter = new BinaryMessageFo rmatter();
>
> incomingMessage (source, new IncomingMessage EventArgs((obje ct)m.BodyStream , > false));
>
> mq.BeginReceive ();
>
> }
>
> catch(Exception ex)
>
> {
>
> Console.WriteLi ne(ex.ToString( ));
>
> }
>
> }
>
>
>
> .....
>
>
>
> public void OnIncomingMessa ge(object source, IncomingMessage EventArgs
> e)
>
> {
>
> BinaryFormatter formatter = new BinaryFormatter ();
>
> application = null;
>
> application =
> (ApplicationInf o)formatter.Des erialize((Strea m)e.getDocument );
>
> }
>
>
>
>
>
>
>
>
>
> "Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
> news:MP******** *************** *@msnews.micros oft.com...
>> Igor <no***@hotmail. com> wrote:
>> > While executing BinaryFormatter .Deserialize() I get:
>> > System.InvalidC astException: 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.co m>
>> 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.c om> wrote in
message news:%2******** **********@tk2m sftngp13.phx.gb l...
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.co m

"Igor" <no***@hotmail. com> wrote in message
news:uo******** ******@TK2MSFTN GP12.phx.gbl...
Earlier I tried only with [Serializable()] attribute, it didnt worked
either. :(
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in
message news:Oc******** ******@TK2MSFTN GP15.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.co m

"Igor" <no***@hotmail. com> wrote in message
news:OJ******** ******@TK2MSFTN GP10.phx.gbl...
> [Serializable()]
>
> public class ApplicationInfo :IDisposable,IS erializable
>
> {
>
> 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 applicationQueu e;
>
> private int machineID;
>
> private int boardID;
>
> private int channelID;
>
> private string applicationName = "";
>
> public ApplicationInfo ()
>
> {
>
> }
>
> public ApplicationInfo (SerializationI nfo info, StreamingContex t ctxt)
>
> {
>
> session = (Guid)info.GetV alue("session", typeof(Guid));
>
> sessionNW = (Guid)info.GetV alue("sessionNW ", typeof(Guid));
>
> ANI = (string)info.Ge tValue("ANI", typeof(string)) ;
>
> DNIS = (string)info.Ge tValue("DNIS", typeof(string)) ;
>
> mqNW = (string)info.Ge tValue("mqNW", typeof(string)) ;
>
> parent = (string)info.Ge tValue("parent" , typeof(string)) ;
>
> child = (string)info.Ge tValue("child", typeof(string)) ;
>
> applicationQueu e = (string)info.Ge tValue("applica tionQueue",
> typeof(string)) ;
>
> applicationName = (string)info.Ge tValue("applica tionName",
> typeof(string)) ;
>
> machineID = (int)info.GetVa lue("machineID" , typeof(int));
>
> boardID = (int)info.GetVa lue("boardID", typeof(int));
>
> channelID = (int)info.GetVa lue("channelID" , typeof(int));
>
> }
>
> public ApplicationInfo (string session, string sessionNW, string ANI,
> string
> DNIS, string mqNW, string parent, string child, string
> applicationQueu e,
> 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.applicatio nQueue = applicationQueu e;
>
> this.machineID = machine;
>
> this.boardID = board;
>
> this.channelID = channel;
>
> this.applicatio nName = applicationName ;
>
> }
>
> #region IDisposable Members
>
> public void Dispose()
>
> {
>
> // TODO: Add ApplicationInfo .Dispose implementation
>
> }
>
> #endregion
>
> #region ISerializable Members
>
> public void GetObjectData(S erializationInf o info, StreamingContex t
> 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(" applicationQueu e", applicationQueu e);
>
> 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.Seria lize(stream, app);
>
>
>
> MessageQueue mq = new MessageQueue(mq Path);
>
> Message m = new Message(stream, new BinaryMessageFo rmatter());
>
> mq.Formatter = new BinaryMessageFo rmatter();
>
> mq.Send(m);
>
>
>
> ........
>
>
>
> public void OnReceiveComple ted(Object source, ReceiveComplete dEventArgs > asyncResult)
>
> {
>
> try
>
> {
>
> MessageQueue mq = (MessageQueue)s ource;
>
> Message m = mq.EndReceive(a syncResult.Asyn cResult);
>
>
>
> m.Formatter = new BinaryMessageFo rmatter();
>
> incomingMessage (source, new

IncomingMessage EventArgs((obje ct)m.BodyStream ,
> false));
>
> mq.BeginReceive ();
>
> }
>
> catch(Exception ex)
>
> {
>
> Console.WriteLi ne(ex.ToString( ));
>
> }
>
> }
>
>
>
> .....
>
>
>
> public void OnIncomingMessa ge(object source, IncomingMessage EventArgs
> e)
>
> {
>
> BinaryFormatter formatter = new BinaryFormatter ();
>
> application = null;
>
> application =
> (ApplicationInf o)formatter.Des erialize((Strea m)e.getDocument );
>
> }
>
>
>
>
>
>
>
>
>
> "Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
> news:MP******** *************** *@msnews.micros oft.com...
>> Igor <no***@hotmail. com> wrote:
>> > While executing BinaryFormatter .Deserialize() I get:
>> > System.InvalidC astException: 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.co m>
>> http://www.pobox.com/~skeet
>> If replying to the group, please do not mail me too
>
>



Nov 16 '05 #10

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

Similar topics

2
3809
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 any hard limit on the size of this object? Is it only limited by the amount of memory or hard-disk space in the server?
5
3835
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 being sent using BinaryFormatter and a serializable object. When using a windows forms application as a client, everything works just fine and deserialized data can be received intact. When the client application is is embedded in Internet Explorer (I...
0
1055
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 SerializationBinder and setting the Binder property of the BinaryFormatter to an instance of that class. The code used was just one line to return the type used to serialize data. I got this information by help of the following Url: ...
3
5482
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 following error: BinaryFormatter Version incompatibility. Expected Version 1.0. Received Version 1986938170.1886350437. I've read a lot about this error from several sources on the net, but can't
0
1771
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
2078
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 application implementation in another .dll (let's call that app.dll). app.dll makes a function call into another dll (let's call that one dep.dll). dep.dll has some code in it where it deserializes a memorystream using a binaryformatter.
19
6250
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 read data from the transport connection." that InnerException of type System.Net.Sockets.SocketException saying "An established connection was aborted by the software in your host machine".
2
5820
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 and continue process the stream. My app reads and writes configuration settings to a binary file upon starting and shutdown. The settings are stored in various objects that are serialized and deserialized using a BinaryFormatter. When reading...
11
3124
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 FileFormatter As New BinaryFormatter() FPs = DirectCast(FileFormatter.Deserialize(FileStream), StuctureDataFile)
0
9663
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9506
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10404
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9979
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9016
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6761
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.