在WLC的Enterprise Library 6中配置故障合同异常处理程序

时间:2013-05-08 19:22:31

标签: wcf exception-handling enterprise-library

在使用Enterprise Library 6的异常处理应用程序块时,如何将异常的其他属性映射到自定义错误契约?

This article以相同的方式this one does描述了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开发人员指南”which can be found here第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映射到我的错误异常中的新属性?

1 个答案:

答案 0 :(得分:0)

事实证明,你不应该在ExceptionManager.Processs()方法中放置你期望异常的代码。我以前这样做过:

exManager.Process(() => wimDAL.Execute_NonQueryNoReturn(sc), "TestPolicy");

Insead,只是正常执行代码。

wimDAL.Execute_NonQueryNoReturn(sc);

这不符合“Microsoft Enterprise Library-Preview.pdf开发人员指南”所说的,但我想文档仍在进行中。我希望这有助于其他人。

相关问题