0

I have the following:

[Serializable]
public class SimulationException : Exception
{
    public SimulationExceptionStatusCode StatusCode { get; set; }

    public SimulationException()
    { }

    public SimulationException(string msg) : base(msg)
    { }

    protected SimulationException(SerializationInfo info, StreamingContext context)
        : base(info, context)
    { }
}

[Serializable]
public enum SimulationExceptionStatusCode
{
    SimulationInstanceNotExist,
    LocationNotExist,
    InvalidOperation,
    GeneralError
}

and I am using the following to convert between fault and exceptions in client-server wcf: Converting Fault to exceptions

The thing is that when I am converting the exception to fault with this:

// converting to error to falut message Fault
MessageFault messageFault = MessageFault.CreateFault(
                new FaultCode("Sender"),
                new FaultReason(error.Message),
                error,
                new NetDataContractSerializer());
fault = Message.CreateMessage(version, messageFault, null);

the enum is not being serializied and when I am deserializing I get the default value for the enum.

What am I missing?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ori Price
  • 3,593
  • 2
  • 22
  • 37

2 Answers2

0

Decorate it with EnumMemeber like so

[DataContract(Name = "SimulationExceptionStatusCode")]
public enum SimulationExceptionStatusCode
{
    [EnumMember]
    SimulationInstanceNotExist,
    [EnumMember]
    LocationNotExist,
    [EnumMember]
    InvalidOperation,
    [EnumMember]
    GeneralError
}
Petar Vučetin
  • 3,555
  • 2
  • 22
  • 31
0

I had the same problem.
I did a simple work-around that might be good for you, when sending the enum, cast to int/string, send the int/string (in the data contract - i passed it in WCF), and then on service, cast the int/string to your enum.

When casting back to enum, use

Enum.Parse(..)
ilansch
  • 4,784
  • 7
  • 47
  • 96
  • I tried to added another int property to my simulationException class. i edited that property, and again i get it with default value of 0. so i guess this is not the solution.... – Ori Price Jun 26 '13 at 16:16
  • This IS a solution, if you miss implemented it, its a problem in your application, Enum is a value type, and it must have a value. in your case the value of GeneralError is "3", instead of sending the enum "SimulationExceptionStatusCode" in the DataContract, send it as string, with the value, and on service side, cast the string back to the enum. This must serialize since we all can serialize string. hence the problem is when you serialize the Exception itself, and not the enum. – ilansch Jun 26 '13 at 18:20
  • by the way, by looking at your code, you are trying to serialize an exception, i believe this is wrong. from my understanding it is not good practice to serialize an exception, send the exception as string. http://stackoverflow.com/questions/486460/how-to-serialize-an-exception-object-in-c – ilansch Jun 26 '13 at 18:24