Danny Ellis Jr.
Danny Ellis Jr.

Reputation: 1706

How do you localize error messages when using Enterprise Library Exception Management?

I'm not very familiar with localization in general and I'm brand new to Microsoft Enterprise Library. How do I create a localized resource and use it in Exception Management?

I've set up a replace handler to throw a friendly error message when exceptions get processed by my policy and there are fields there for specifying Message Resource Name and Message Resource Type. I have no idea what goes in these fields. I can find no examples whatsoever.

Upvotes: 0

Views: 609

Answers (2)

Jesse
Jesse

Reputation: 151

You don't have to edit the Designer file manually; in fact, you shouldn't.

I'm using VS 2010 and C#, but the concepts should be the same for VS 2012 and VB.

The following link was very helpful to me, in addition to your answer, to figuring this out:

http://odetocode.com/blogs/scott/archive/2009/07/16/resource-files-and-asp-net-mvc-projects.aspx

(You can ignore his warnings about App_GlobalResources -- that's where I have my Exceptions.resx file and this works for me.)

  1. Make sure the Enterprise Library Configuration tool is closed.
  2. Open the Designer.vb (or Designer.cs) file so you can observe the changes happen.
  3. Right-click on your .resx file and select Properties.
  4. Change Build Action to Embedded Resource.
  5. Change Custom Tool to PublicResXFileCodeGenerator. This ensures that the class is generated as public, not internal (or Friend in VB). After you click away from this property, you should see your resource class is now defined as public in the Designer file.
  6. Set Custom Tool Namespace to something meaningful, i.e. <ApplicationName>.Resources. Observe that the namespace has changed in the Designer file.
  7. Build your project to get a new version of your assembly.
  8. Edit your EntLib configuration file in the Configuration Tool, expand the Exception Handling Settings, then expand the Wrap or Replace Handler you want to configure.
  9. Click the ellipsis (...) at the end of the Message Resource Type field. This brings up the Type Browser. Click Add from File, browse to the bin folder of your project, and select your project's main assembly. The type should now appear in the Loaded assemblies.
  10. Expand the assembly to find the namespace you entered above, expand that, select the type that has been created for your exception messages, and click OK.
  11. Set the Message Resource Name to the key in the resource file that holds the message for this exception.

Upvotes: 1

Danny Ellis Jr.
Danny Ellis Jr.

Reputation: 1706

I figured this out.

You add standard resources in .resx files just like any other localization. My problem was that when I went to add the Resource Type and Resource Name to my Replace Handler in Enterprise Library's Configuration interface, the assembly did not contain any Types to add.

The problem is two things. When you create a .resx in Visual Studio 2012 using VB.Net, it uses My.Resources as the Namespace. It could be My.[FolderName], I don't know. The other problem is that it creates the class as Friend, not Public.

I had to change the Namespace in properties and manually change Friend to Public in the .vb file. It changes back whenever I add a new resource because it regenerates the code, but at least now I can make it work.

Upvotes: 0

Related Questions