1

Enterprise Library 6 の例外処理アプリケーション ブロックを使用する場合、例外の追加プロパティをカスタム フォールト コントラクトにどのようにマップしますか?

この記事では、これと同じ方法で FaultContractPropertyMapping について説明ます。次のような障害契約がある場合:

[DataContract]
public class SalaryCalculationFault
{
  [DataMember]
  public Guid FaultID { get; set; }

  [DataMember]
  public string FaultMessage { get; set; }
}

別のプロパティを追加して元の例外にマップするにはどうすればよいですか? 新しいプロパティを使用してストアド プロシージャ名をクライアントに表示したいとします。

[DataMember]
public string StoredProcedureName { get; set; }

ここにある「Microsoft Enterprise Library-Preview.pdf の開発者ガイド」の 90 ページに示されているマッピングを編集しようとしましたが、うまくいかないようです。私の新しいマッピングは次のようになります。

var mappings = new NameValueCollection();
mappings.Add("FaultID", "{Guid}");
mappings.Add("FaultMessage", "{Message}");
mappings.Add("StoredProcedureName", "{Procedure}");   //SqlException has a Procedure property

そして、ここにポリシーがあります。

var testPolicy = new List<ExceptionPolicyEntry>
{
    {
        new ExceptionPolicyEntry(typeof(SqlException), 
            PostHandlingAction.ThrowNewException, 
            new IExceptionHandler[]
            {
                new FaultContractExceptionHandler(typeof(SalaryCalculationFault), mappings)
            }) 
    }
};

var policies = new List<ExceptionPolicyDefinition>();
policies.Add(new ExceptionPolicyDefinition(
    "TestPolicy", testPolicy));

exManager = new ExceptionManager(policies);
ExceptionPolicy.Reset();
ExceptionPolicy.SetExceptionManager(exManager);

これを行い、クライアントで FaultException をキャッチして検査すると、StoredProcedureName は常に空です。SqlException から障害例外の新しいプロパティにマップされないのはなぜですか?

4

1 に答える 1