2017-06-30 5 views
1

Azureサービスバスで例外をシリアル化する方法は?私は、応答キューにWebJobの実行の結果として例外を送信しようとしている

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

私の例外クラスは、論理的なエラーを表します。

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; } 
} 

はしかし、私は次の取得します例外:私は[DataContract]MyExceptionをマークするとき

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.

そして、私はこれを取得:

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.ExceptionISerializableを実装しているためです。どうすれば修正できますか?

+0

"応答[...]を応答キューに送信する" - 受信者は本当にException *オブジェクト全体を必要としますか?あるいは、 '.ToString()'やステータスコード、またはカスタムメッセージパケットを使うほうが良いでしょうか? – AakashM

+0

UIのエラーメッセージを適切にフォーマットするために追加の情報(私の例ではMyProp)を渡す必要があるので、私は文字列や列挙型を使うことができません。 – UserControl

答えて

4

DataContractSerializerで例外をシリアライズすることはできません。クラスをDataContractで正常にマークしたとしても、内部例外はどのタイプでもかまいません。したがって互換性はありません。

  1. は別のシリアライザを使用して例外をシリアル化(そしてStreamBrokeredMessageコンストラクタを渡す):

    は、だから、2つのオプションがあります。いくつかのオプションについては、this questionを参照してください。

  2. 私はワイヤを介して例外自体を送信することはあまりクリーンではないと主張します。むしろ、消費者にとって重要なすべての情報を含むカスタムメッセージを作成し、このメッセージをサービスバスに送信したいと思います。

関連する問題