How To Use Exception Manager Enterprise Library 6.0

孤人 提交于 2019-11-29 01:14:02

问题


When using Enterprise Library 6.0, this error occurs in the code below:

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

"Must set an ExceptionManager in the ExceptionPolicy class using the SetExceptionManager method."

In Enterprise Library 5.0 this code worked:

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

But in Enterprise Library 6.0 the EnterpriseLibraryContainer class is not found. I want get instance of ExceptionManager. How do I solve this problem ?


回答1:


EnterpriseLibraryContainer was removed for the release of Enterprise Library 6. There is a new approach for bootstrapping the application blocks in Enterprise Library 6. If you want to get an instance of ExceptionManager you can use the factory:

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

ExceptionManager exManager = factory.CreateManager();

To configure the blocks to use the static facades you can use the SetExceptionManager method and supply the ExceptionManager from above:

ExceptionPolicy.SetExceptionManager(factory.CreateManager());

This only needs to be done once at application startup.




回答2:


I also faced this issue and now i've solved this. So, you can also try to set the following code in the Application_Start() in the Global.asax file:

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());



回答3:


If you want to get the HandleException in a boolean variable, you must only access the ExceptionManager.

exManager.HandleException(ex, "ReplacePolicy1");

this is example complete:

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


来源:https://stackoverflow.com/questions/17131190/how-to-use-exception-manager-enterprise-library-6-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!