如何序列化 Azure 服务总线中的异常?
How to serialize an exception in Azure Service Bus?
我正在尝试将 WebJob 执行的异常发送到响应队列:
var message = new BrokeredMessage(exception);
outputMessages.Add(message);
我的异常class表示逻辑错误:
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; }
}
但是,我得到以下异常:
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.
当我用 [DataContract]
标记 MyException
时,我得到了这个:
Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception
while executing function: Functions.ProcessQueueMessage2Async --->
System.Runtime.Serialization.InvalidDataContractException: Type
'MyException' cannot be ISerializable and have DataContractAttribute
attribute.
可能是因为 System.Exception
实现了 ISerializable
。我该如何解决?
您将无法使用 DataContractSerializer
序列化您的异常。即使您成功地将 class 标记为 DataContract
,内部异常也可以是任何类型,因此不兼容。
所以你有两个选择:
使用另一个序列化程序序列化您的异常(然后将 Stream
传递给 BrokeredMessage
构造函数)。有关某些选项,请参阅 this question。
我认为通过网络发送异常本身不是很干净。我宁愿创建一个自定义消息,其中包含对消费者很重要的所有信息,然后将此消息发送到服务总线。
我正在尝试将 WebJob 执行的异常发送到响应队列:
var message = new BrokeredMessage(exception);
outputMessages.Add(message);
我的异常class表示逻辑错误:
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; }
}
但是,我得到以下异常:
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.
当我用 [DataContract]
标记 MyException
时,我得到了这个:
Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Functions.ProcessQueueMessage2Async ---> System.Runtime.Serialization.InvalidDataContractException: Type 'MyException' cannot be ISerializable and have DataContractAttribute attribute.
可能是因为 System.Exception
实现了 ISerializable
。我该如何解决?
您将无法使用 DataContractSerializer
序列化您的异常。即使您成功地将 class 标记为 DataContract
,内部异常也可以是任何类型,因此不兼容。
所以你有两个选择:
使用另一个序列化程序序列化您的异常(然后将
Stream
传递给BrokeredMessage
构造函数)。有关某些选项,请参阅 this question。我认为通过网络发送异常本身不是很干净。我宁愿创建一个自定义消息,其中包含对消费者很重要的所有信息,然后将此消息发送到服务总线。