Click here to Skip to main content
15,878,871 members
Home / Discussions / C#
   

C#

 
AnswerRe: C# Convertion Pin
OriginalGriff26-Mar-13 21:33
mveOriginalGriff26-Mar-13 21:33 
GeneralRe: C# Convertion Pin
Richard MacCutchan26-Mar-13 22:54
mveRichard MacCutchan26-Mar-13 22:54 
GeneralRe: C# Convertion Pin
OriginalGriff26-Mar-13 22:57
mveOriginalGriff26-Mar-13 22:57 
GeneralRe: C# Convertion Pin
Keith Barrow26-Mar-13 23:51
professionalKeith Barrow26-Mar-13 23:51 
AnswerRe: C# Convertion Pin
V.26-Mar-13 22:29
professionalV.26-Mar-13 22:29 
QuestionLinq To SQL Left Join Pin
Kevin Marois26-Mar-13 17:01
professionalKevin Marois26-Mar-13 17:01 
AnswerRe: Linq To SQL Left Join Pin
Simon_Whale26-Mar-13 23:19
Simon_Whale26-Mar-13 23:19 
Questionserialization / type cast differs between service and console execution Pin
JudyL_MD26-Mar-13 14:20
JudyL_MD26-Mar-13 14:20 
I've got a service than runs on two machines and communicates back and forth through a socket connection.
To make debugging easier, I've wrapped the OnStart and OnStop stuff inside a console program. The only
difference in the two running instances is a) how the exe starts (SCM versus double-clicking the console exe) and b) the account the exe runs in (LocalSystem versus current user). Actual code of the "guts" of the program is the same.

When the following runs with two consoles communicating, everything is fine. When it runs with a console app on one machine talking to a service on the other, it fails on the indicated line, throwing a System.InvalidCastException with the message unable to cast object of type 'OverarchingServer.DerivedMessage' to type 'OverarchingServer.DerivedMessage'

A breakpoint in the debugger of the running service at the line in question shows that msg IS of the expected 'DerivedMessage' type and the fields are fully and correctly populated. The exception thrown indicates the types ARE the same. So why can't I do the type cast?

I haven't tried service to service yet - that is rather difficult to set up on my development system. Besides, I want to know why this doesn't work when run this way.

Any ideas?

Thanks,
Judy

namespace ClientMessages
{
    [Serializable]
    abstract public class BaseMessageClass
    {
        // common stuff of type int, string and an enum
        public int type;
    }
}

namespace OverarchingServer
{
    using ClientMessages;

    public enum MessageTypeEnum : int
    {
        DerivedMessageType = 1,
        AnotherType,
        YetAnotherType
    }

    [Serializable]
    public class DerivedMessage : BaseMessageClass
    {
        // specific stuff including a structure { Guid, string, int } and an enum
    }

    public class SendingServer
    {
        private Socket client;      // opened in code not shown

        public void DoingWork ()
        {
            DerivedMessage msg = new DerivedMessage ();
            // fill the fields
            this.Send (msg);
        }

        public void Send (BaseMessageClass msg)
        {
            MemoryStream mem = new MemoryStream ();
            BinaryFormatter bf = new BinaryFormatter ();

            bf.Serialize (mem, msg);
            byte[] data = mem.ToArray ();

            NetworkStream stream = new NetworkStream (this.client);
            BinaryWriter binWrite = new BinaryWriter (stream);

            binWrite.Write ((Int32) data.Length);
            binWrite.Write (data, 0, data.Length);
        }
    }

    public class ReceivingServer
    {
        private Socket client;  // opened in code not shown

        private void LoopWithDetailsOmitted ()
        {
            NetworkStream stream = new NetworkStream (this.client);
            BinaryReader binRead = new BinaryReader (stream);

            int length;
            byte[] data;

            length = binRead.ReadInt32 ();
            data = binRead.ReadBytes (length);

            MemoryStream mem = new MemoryStream (data);
            BinaryFormatter bf = new BinaryFormatter ();

            BaseMessageClass msg = (BaseMessageClass) bf.Deserialize (mem);

            this.HandleMessage (msg);
        }

        private void HandleMessage (BaseMessageClass msg)
        {
            MessageTypeEnum type = (MessageTypeEnum) msg.type;

            switch (type)
            {
                case MessageTypeEnum.DerivedMessageType:
                    this.HandleMessage ((DerivedMessage) msg);      // *****
                    break;
            }
        }

        private void HandleMessage (DerivedMessage msg) { }
    }
}

Be wary of strong drink. It can make you shoot at tax collectors - and miss.
Lazarus Long, "Time Enough For Love" by Robert A. Heinlein

GeneralRe: serialization / type cast differs between service and console execution Pin
JudyL_MD26-Mar-13 14:22
JudyL_MD26-Mar-13 14:22 
AnswerRe: serialization / type cast differs between service and console execution Pin
Dave Kreskowiak26-Mar-13 14:39
mveDave Kreskowiak26-Mar-13 14:39 
QuestionRe: serialization / type cast differs between service and console execution Pin
JudyL_MD26-Mar-13 15:13
JudyL_MD26-Mar-13 15:13 
AnswerRe: serialization / type cast differs between service and console execution Pin
Mycroft Holmes26-Mar-13 15:34
professionalMycroft Holmes26-Mar-13 15:34 
GeneralRe: serialization / type cast differs between service and console execution Pin
JudyL_MD26-Mar-13 16:21
JudyL_MD26-Mar-13 16:21 
GeneralRe: serialization / type cast differs between service and console execution Pin
Mycroft Holmes26-Mar-13 16:49
professionalMycroft Holmes26-Mar-13 16:49 
GeneralRe: serialization / type cast differs between service and console execution Pin
Dave Kreskowiak26-Mar-13 17:02
mveDave Kreskowiak26-Mar-13 17:02 
GeneralRe: serialization / type cast differs between service and console execution Pin
JudyL_MD27-Mar-13 1:42
JudyL_MD27-Mar-13 1:42 
GeneralRe: serialization / type cast differs between service and console execution Pin
Dave Kreskowiak27-Mar-13 1:48
mveDave Kreskowiak27-Mar-13 1:48 
GeneralRe: serialization / type cast differs between service and console execution Pin
JudyL_MD27-Mar-13 1:52
JudyL_MD27-Mar-13 1:52 
Questiondevexpress winforms controls Pin
naseer86126-Mar-13 11:16
naseer86126-Mar-13 11:16 
AnswerRe: devexpress winforms controls Pin
Dave Kreskowiak26-Mar-13 11:43
mveDave Kreskowiak26-Mar-13 11:43 
AnswerRe: devexpress winforms controls Pin
Pete O'Hanlon26-Mar-13 12:09
mvePete O'Hanlon26-Mar-13 12:09 
AnswerRe: devexpress winforms controls Pin
NotPolitcallyCorrect26-Mar-13 14:45
NotPolitcallyCorrect26-Mar-13 14:45 
AnswerRe: devexpress winforms controls Pin
Mycroft Holmes26-Mar-13 15:29
professionalMycroft Holmes26-Mar-13 15:29 
QuestionDirectoryInfo does not return full path Pin
MumbleB26-Mar-13 9:43
MumbleB26-Mar-13 9:43 
AnswerRe: DirectoryInfo does not return full path Pin
Alan N26-Mar-13 11:18
Alan N26-Mar-13 11:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.