Morteza Mousavi
Morteza Mousavi

Reputation: 223

How To Use Exception Manager Enterprise Library 6.0

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 ?

Upvotes: 15

Views: 23976

Answers (3)

Jonathan Burgos G.
Jonathan Burgos G.

Reputation: 344

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

Upvotes: 0

Nimit Joshi
Nimit Joshi

Reputation: 1046

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

Upvotes: 4

Randy Levy
Randy Levy

Reputation: 22655

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.

Upvotes: 21

Related Questions