1
votes

I'm trying to send an exception as a result of a WebJob execution to a response queue:

        var message = new BrokeredMessage(exception);
        outputMessages.Add(message);

My exception class represents a logical error:

public class MyException : Exception{
    public MyException(){}
    public MyException(string message) : base(message){}
    public MyException(string message, Exception inner) : base(message, inner){}
    protected MyException(SerializationInfo info, StreamingContext context) : base(info, context){}

    public object MyProp { get; set; }
}

However, I get the following exception:

Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Functions.ProcessQueueMessage2Async ---> System.Runtime.Serialization.InvalidDataContractException: Type 'MyException' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

And when I mark MyException with [DataContract] I get this:

Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Functions.ProcessQueueMessage2Async ---> System.Runtime.Serialization.InvalidDataContractException: Type 'MyException' cannot be ISerializable and have DataContractAttribute attribute.

Probably because System.Exception implements ISerializable. How can I fix that?

1
"send an exception [...] to a response queue" - does the recipient really need the whole Exception object? Or would it be better off with the .ToString(), or a status code, or some custom message packet?AakashM
I'm afraid I can't go with a string or enum because I need to pass additional information (MyProp in my example) to properly format error messages on UI.UserControl

1 Answers

4
votes

You won't be able to serialize your exception with DataContractSerializer. Even if you marked your class with DataContract successfully, the inner exception can be of any type, thus not compatible.

So you have two options:

  1. Serialize your exception with another serializer (and then pass the Stream to BrokeredMessage constructor). See this question for some options.

  2. I would argue that sending the exception itself over the wire isn't very clean. I'd rather create a custom message which contains all the information that is important to consumers, and then send this message to Service Bus.