如何使用异常管理企业库6.0

14

使用Enterprise Library 6.0时,以下代码会出现错误:

bool rethrow = ExceptionPolicy.HandleException(ex, "ReplacePolicy1")

必须使用SetExceptionManager方法在ExceptionPolicy类中设置一个ExceptionManager。

在Enterprise Library 5.0中,此代码可用:

public static bool HandleException(Exception exception, string PolicyName)
{
    ExceptionManager exManager = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
    ExceptionPolicy.SetExceptionManager(exManager);
    bool rethrow = ExceptionPolicy.HandleException(ex, "ReplacePolicy1");
    return reThrow;
}

但是在Enterprise Library 6.0中,找不到EnterpriseLibraryContainer类。我想获取ExceptionManager的实例。我该如何解决这个问题?


3
大多数,如果不是全部,Enterprise Library 的类已不再得到维护。我不喜欢 EntLib,但我认为这个说法不准确 - 请参见http://msdn.microsoft.com/en-us/library/ff648951.aspx。 - Joe
1
企业库团队在新的企业库6中弃用了缓存、加密和安全块。他们这样做是因为他们认为.NET Framework现在支持许多相同的功能,已经内置了这些功能。他们添加了一些新的块,包括语义日志记录(非常方便)。企业库作为开源项目在entlib.codeplex.com上得到维护,它绝对没有被淘汰。 - Scott Wylie
3个回答

20

为了发布企业库6,已删除EnterpriseLibraryContainer。在Enterprise Library 6中有一种新的方法用于引导应用程序块。如果您想获取ExceptionManager的实例,可以使用工厂:

IConfigurationSource config = ConfigurationSourceFactory.Create();
ExceptionPolicyFactory factory = new ExceptionPolicyFactory(config);

ExceptionManager exManager = factory.CreateManager();

要配置块以使用静态 facades,您可以使用 SetExceptionManager 方法,并提供上面的 ExceptionManager

ExceptionPolicy.SetExceptionManager(factory.CreateManager());

这只需要在应用程序启动时完成一次。


我复制了这段代码,但是在factory.CreateManager()执行时,第三行出现了异常。异常信息为:“必须使用SetExceptionManager方法在ExceptionPolicy类中设置一个ExceptionManager。”有什么解决办法吗? - Philippe
1
只有在调用 ExceptionPolicy.HandleException 时才会抛出该消息。如果您正在使用静态门面,则需要在应用程序启动时(或在异常处理代码执行之前的某个时间)调用 ExceptionPolicy.SetExceptionManager(factory.CreateManager()) 一次。如果仍然遇到问题,则最好发布一个新问题,说明您的问题具体情况(代码、配置等)。 - Randy Levy
谢谢您的回复。我通过实验发现,这只是WCF服务库中的一个问题。当我将代码移动到HTTP托管项目时,问题就解决了。唯一的缺点是,在直接在库上使用WcfSvhHost.exe进行调试时,我无法使用WCF异常屏蔽功能。没关系,这不是什么大问题。 - Philippe
需要这些命名空间: using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling; - Jereme
1
要使用上面代码中设置的ExceptionManager,请使用ExceptionPolicy.HandleException()。 - Jason Cheng

4
我也遇到了这个问题,现在我已经解决了。因此,您也可以尝试在Global.asax文件的Application_Start()中设置以下代码:
IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory());
if (configurationSource.GetSection(LoggingSettings.SectionName) != null)
Logger.SetLogWriter(new LogWriterFactory(configurationSource).Create());
ExceptionPolicy.SetExceptionManager(new ExceptionPolicyFactory(configurationSource).CreateManager());

需要使用以下命名空间: using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling; using Microsoft.Practices.EnterpriseLibrary.Logging; - Jereme

0
如果您想将HandleException存储在布尔变量中,您只需访问ExceptionManager即可。
exManager.HandleException(ex, "ReplacePolicy1");

这是一个完整的例子:

public static bool HandleException(Exception exception, string PolicyName)
{
    IConfigurationSource config = ConfigurationSourceFactory.Create();
    ExceptionPolicyFactory factory = new ExceptionPolicyFactory(config);
    ExceptionManager exManager = factory.CreateManager();
    ExceptionPolicy.SetExceptionManager(factory.CreateManager());               
    bool rethrow  = exManager.HandleException(ex, "ReplacePolicy1");  
    return rethrow;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接