BinaryFormatter.Deserialize

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Igor

    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.


  • Jon Skeet [C# MVP]

    #2
    Re: BinaryFormatter .Deserialize

    Igor <noone@hotmail. com> wrote:[color=blue]
    > While executing BinaryFormatter .Deserialize() I get:
    > System.InvalidC astException: Specified cast is not valid.
    > I implemented ISerializable interface. What may be a problem?[/color]

    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 - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Igor

      #3
      Re: BinaryFormatter .Deserialize

      [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]" <skeet@pobox.co m> wrote in message
      news:MPG.1bd5c5 ac27ec3fb598b67 a@msnews.micros oft.com...[color=blue]
      > Igor <noone@hotmail. com> wrote:[color=green]
      > > While executing BinaryFormatter .Deserialize() I get:
      > > System.InvalidC astException: Specified cast is not valid.
      > > I implemented ISerializable interface. What may be a problem?[/color]
      >
      > 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 - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: BinaryFormatter .Deserialize

        Igor <noone@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 - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Igor

          #5
          Re: BinaryFormatter .Deserialize

          [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]" <skeet@pobox.co m> wrote in message
          news:MPG.1bd5d2 c68294d30898b68 1@msnews.micros oft.com...[color=blue]
          > Igor <noone@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 - <skeet@pobox.co m>
          > http://www.pobox.com/~skeet
          > If replying to the group, please do not mail me too[/color]


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: BinaryFormatter .Deserialize

            Igor <noone@hotmail. com> wrote:

            <snip>

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

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Nicholas Paldino [.NET/C# MVP]

              #7
              Re: BinaryFormatter .Deserialize

              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]
              - mvp@spam.guard. caspershouse.co m

              "Igor" <noone@hotmail. com> wrote in message
              news:OJRpa8EsEH A.3964@TK2MSFTN GP10.phx.gbl...[color=blue]
              > [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]" <skeet@pobox.co m> wrote in message
              > news:MPG.1bd5c5 ac27ec3fb598b67 a@msnews.micros oft.com...[color=green]
              >> Igor <noone@hotmail. com> wrote:[color=darkred]
              >> > While executing BinaryFormatter .Deserialize() I get:
              >> > System.InvalidC astException: Specified cast is not valid.
              >> > I implemented ISerializable interface. What may be a problem?[/color]
              >>
              >> 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 - <skeet@pobox.co m>
              >> http://www.pobox.com/~skeet
              >> If replying to the group, please do not mail me too[/color]
              >
              >[/color]


              Comment

              • Igor

                #8
                Re: BinaryFormatter .Deserialize

                Earlier I tried only with [Serializable()] attribute, it didnt worked
                either. :(


                "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
                message news:OcYikkFsEH A.3896@TK2MSFTN GP15.phx.gbl...[color=blue]
                > Igor,
                >
                > While I haven't taken the time to run the code, I can see that there[/color]
                is[color=blue]
                > 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]
                > - mvp@spam.guard. caspershouse.co m
                >
                > "Igor" <noone@hotmail. com> wrote in message
                > news:OJRpa8EsEH A.3964@TK2MSFTN GP10.phx.gbl...[color=green]
                > > [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[/color][/color]
                IncomingMessage EventArgs((obje ct)m.BodyStream ,[color=blue][color=green]
                > > 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]" <skeet@pobox.co m> wrote in message
                > > news:MPG.1bd5c5 ac27ec3fb598b67 a@msnews.micros oft.com...[color=darkred]
                > >> Igor <noone@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 - <skeet@pobox.co m>
                > >> http://www.pobox.com/~skeet
                > >> If replying to the group, please do not mail me too[/color]
                > >
                > >[/color]
                >
                >[/color]


                Comment

                • Nicholas Paldino [.NET/C# MVP]

                  #9
                  Re: BinaryFormatter .Deserialize

                  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]
                  - mvp@spam.guard. caspershouse.co m

                  "Igor" <noone@hotmail. com> wrote in message
                  news:uofJ8qFsEH A.4032@TK2MSFTN GP12.phx.gbl...[color=blue]
                  > Earlier I tried only with [Serializable()] attribute, it didnt worked
                  > either. :(
                  >
                  >
                  > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
                  > in
                  > message news:OcYikkFsEH A.3896@TK2MSFTN GP15.phx.gbl...[color=green]
                  >> Igor,
                  >>
                  >> While I haven't taken the time to run the code, I can see that there[/color]
                  > is[color=green]
                  >> 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]
                  >> - mvp@spam.guard. caspershouse.co m
                  >>
                  >> "Igor" <noone@hotmail. com> wrote in message
                  >> news:OJRpa8EsEH A.3964@TK2MSFTN GP10.phx.gbl...[color=darkred]
                  >> > [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[/color][/color]
                  > IncomingMessage EventArgs((obje ct)m.BodyStream ,[color=green][color=darkred]
                  >> > 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]" <skeet@pobox.co m> wrote in message
                  >> > news:MPG.1bd5c5 ac27ec3fb598b67 a@msnews.micros oft.com...
                  >> >> Igor <noone@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 - <skeet@pobox.co m>
                  >> >> http://www.pobox.com/~skeet
                  >> >> If replying to the group, please do not mail me too
                  >> >
                  >> >[/color]
                  >>
                  >>[/color]
                  >
                  >[/color]


                  Comment

                  • Igor

                    #10
                    Re: BinaryFormatter .Deserialize

                    Thats what I did and I got the same exception


                    "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
                    message news:%23A%23a8v FsEHA.1712@tk2m sftngp13.phx.gb l...[color=blue]
                    > 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]
                    > - mvp@spam.guard. caspershouse.co m
                    >
                    > "Igor" <noone@hotmail. com> wrote in message
                    > news:uofJ8qFsEH A.4032@TK2MSFTN GP12.phx.gbl...[color=green]
                    > > Earlier I tried only with [Serializable()] attribute, it didnt worked
                    > > either. :(
                    > >
                    > >
                    > > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
                    > > in
                    > > message news:OcYikkFsEH A.3896@TK2MSFTN GP15.phx.gbl...[color=darkred]
                    > >> Igor,
                    > >>
                    > >> While I haven't taken the time to run the code, I can see that[/color][/color][/color]
                    there[color=blue][color=green]
                    > > is[color=darkred]
                    > >> nothing in your class that requires custom serialization. All you have
                    > >> to
                    > >> do is add the Serializable attribute, and your serialization should[/color][/color][/color]
                    work[color=blue][color=green][color=darkred]
                    > >> fine. ISerializable only needs to be implemented when there are other
                    > >> things (besides the fields in your class) that you want to serialize,[/color][/color][/color]
                    or[color=blue][color=green][color=darkred]
                    > >> they require certain logic to be executed.
                    > >>
                    > >> Hope this helps.
                    > >>
                    > >>
                    > >> --
                    > >> - Nicholas Paldino [.NET/C# MVP]
                    > >> - mvp@spam.guard. caspershouse.co m
                    > >>
                    > >> "Igor" <noone@hotmail. com> wrote in message
                    > >> news:OJRpa8EsEH A.3964@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,[/color][/color][/color]
                    ReceiveComplete dEventArgs[color=blue][color=green][color=darkred]
                    > >> > asyncResult)
                    > >> >
                    > >> > {
                    > >> >
                    > >> > try
                    > >> >
                    > >> > {
                    > >> >
                    > >> > MessageQueue mq = (MessageQueue)s ource;
                    > >> >
                    > >> > Message m = mq.EndReceive(a syncResult.Asyn cResult);
                    > >> >
                    > >> >
                    > >> >
                    > >> > m.Formatter = new BinaryMessageFo rmatter();
                    > >> >
                    > >> > incomingMessage (source, new[/color]
                    > > IncomingMessage EventArgs((obje ct)m.BodyStream ,[color=darkred]
                    > >> > 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]" <skeet@pobox.co m> wrote in message
                    > >> > news:MPG.1bd5c5 ac27ec3fb598b67 a@msnews.micros oft.com...
                    > >> >> Igor <noone@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 - <skeet@pobox.co m>
                    > >> >> http://www.pobox.com/~skeet
                    > >> >> If replying to the group, please do not mail me too
                    > >> >
                    > >> >
                    > >>
                    > >>[/color]
                    > >
                    > >[/color]
                    >
                    >[/color]


                    Comment

                    • Joep

                      #11
                      Re: BinaryFormatter .Deserialize

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

                      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...



                      Comment

                      • Igor

                        #12
                        Re: BinaryFormatter .Deserialize

                        object obj = formatter.Deser ialize((MemoryS tream)e.getDocu ment);

                        ApplicationInfo .ApplicationInf o application = (ApplicationInf o.ApplicationIn fo)obj;

                        I get that exception in second row.



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

                        {

                        [Serializable()]

                        public class ApplicationInfo :IDisposable

                        {

                        ...

                        }
                        }


                        "Joep" <Staat@DeStoep. nl> wrote in message news:416ce49d$0 $559$e4fe514c@n ews.xs4all.nl.. .[color=blue]
                        > application = (ApplicationInf o)formatter.Des erialize((Strea m)e.getDocument );
                        >
                        > 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...
                        >
                        >
                        >[/color]

                        Comment

                        Working...