Can we set the ExceptionMessage in replace handler using resource files?

Topics: Exception Handling Application Block
Aug 31, 2009 at 11:21 AM

Can we set the ExceptionMessage in replace handler (Enterprise library) using resource files? if yes how?

I have an application named ResourceExample. This has a App_GlobalResources folder containing the MyResourceabc.resx which contains mapping of 

 "ErrorMessage" to "Server not found"

In the Default.aspx, i am throwin this error on button click for testing,

 

 ArgumentNullException ex = new ArgumentNullException("ErrorMessage");

throw ex;

But its not picking the Error String from the resource file!!

The config file is as follows:

<exceptionHandling>
    <exceptionPolicies>
      <add name="ExceptionPolicy">
        <exceptionTypes>
          <add type="System.ArgumentNullException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException" name="ArgumentNullException">
            <exceptionHandlers>
              <add exceptionMessage="" exceptionMessageResourceName="MyResourceabc"
                exceptionMessageResourceType="Resources.MyResourceabc, App_GlobalResources.ha-7fil4, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
                replaceExceptionType="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                name="Replace Handler" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>

Please let me know if there is some thing which i did wrong...

Thanks in advance for the help,

psrikara.

Sep 1, 2009 at 3:01 AM

There's something wrong with the value of the exceptionMessageResourceType.  It should be the full typename of your resource file.  For example, if you placed your resx in the App_GlobalResources folder and your project namespace is MyWebApplication, the exceptionMessageResourceType value should be MyWebApplication.App_GlobalResources.MyResourceabc, MyWebApplciation, .....version info,etc.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Aug 12, 2010 at 1:48 PM

My Web Application project name: MyCompany.MyProject.UI.MySite
My resource name in the App_GlobalResources: Messages

I want to change the message of the exception from resource, but the following configuration doesn't work:

 

<add name="User Interface Exception Policy">
	<exceptionTypes>
		<add name="DatabaseNotFoundException" type="MyCompany.General.Web.Exceptions.UserFirendly.DatabaseNotFoundException, MyCompany.General.Web.Exceptions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" postHandlingAction="None">
			<exceptionHandlers>
				<add name="Replace Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
				exceptionMessageResourceName="DatabaseNotFound" 
				exceptionMessageResourceType="MyCompany.MyProject.UI.MySite.App_GlobalResources.Messages, MyCompany.MyProject.UI.MySite" replaceExceptionType="MyCompany.General.Web.Exceptions.UserInterfaceException, MyCompany.General.Web.Exceptions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
			</exceptionHandlers>
		</add>
	</exceptionTypes>
</add>

What is wrong with this configuration?

  I could not select the Resource from the Configuraiton Tool and I see that the generated class in the resx.cs is internal.

 

Aug 13, 2010 at 1:30 AM

If you directly add a resource file under the App_GlobalResources folder, its Build Action property is set to Content by default.  Change it to Embedded Resource.   Regarding the exceptionMessageResourceType, make sure it's correct.  The default namespace of a resource file under App_GlobalResources is Resources, so if you didn't change anything, your exceptionMessageResourceType  should be:

Resources.Messages, MyCompany.MyProject.UI.MySite

assuming MyCompany.MyProject.UI.MySite is the assembly name.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Aug 13, 2010 at 2:53 PM

Thanks! It is now working! I changed the namespace to Resources.Messages and set the postHandlingAction to ThrowNewException from None.