14

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 ?

Randy Levy
  • 22,566
  • 4
  • 68
  • 94
Morteza Mousavi
  • 213
  • 1
  • 4
  • 11
  • 3
    "Most, if not all, of the Enterprise Library classes are no longer maintained" - I'm not a fan of EntLib, but I don't think this statement is accurate - see http://msdn.microsoft.com/en-us/library/ff648951.aspx – Joe Jun 16 '13 at 14:43
  • 1
    Enterprise Library team deprecated the Caching, Cryptography and Security block in the new Enterprise Library 6. They did this as they felt that the .NET Framework supported many of the same features now out of the box. They did add a few new blocks including Semantic Logging (which is very handy). Enterprise Library is maintained as open source on entlib.codeplex.com and it definitely is not retired. – Scott Wylie Aug 07 '13 at 23:58

3 Answers3

20

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.

Randy Levy
  • 22,566
  • 4
  • 68
  • 94
  • I copied this code but I am getting an exception on the 3rd line when the factory.CreateManager() executes. The exception message is: "Must set an ExceptionManager in the ExceptionPolicy class using the SetExceptionManager method." Any ideas on how to resolve this? – Philippe Jul 19 '13 at 19:51
  • 1
    That message is only thrown when calling `ExceptionPolicy.HandleException`. If you are using the static facade then you need to call `ExceptionPolicy.SetExceptionManager(factory.CreateManager());` once in application startup (or sometime before your exception handling code executes). If you are still having problems then it's best to post a new question with the specifics of your issue (code, configuration, etc.). – Randy Levy Jul 19 '13 at 19:59
  • Thank you for your response. I just found out by experimenting that this is only an issue in WCF service libraries. When I move the code to the HTTP hosting project, the problem goes away. The only downfall is that I don't exercise the WCF exception shielding when debugging using WcfSvhHost.exe on the library directly. No big deal. – Philippe Jul 19 '13 at 20:50
  • Requires these namespaces: using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling; – Jereme May 08 '19 at 19:20
  • 1
    To use the ExceptionManager that is set in the above code, use ExceptionPolicy.HandleException() – Jason Cheng Aug 05 '19 at 17:21
4

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());
Nimit Joshi
  • 1,026
  • 3
  • 19
  • 46
  • Requires these name spaces: using Microsoft.Practices.EnterpriseLibrary.Common.Configuration; using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling; using Microsoft.Practices.EnterpriseLibrary.Logging; – Jereme May 08 '19 at 19:20
0

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