Tuesday, November 6, 2007

Idiot's Guide to Implementing Microsoft.Practices.EnterpriseLibrary.ExceptionHandling

A simple tutorial for VB.NET folks.

Get Enterprise Library 3.0

Install.

In your app, add reference to C:\Program Files\Microsoft Enterprise Library 3.1—May 2007\Bin

Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll
Microsoft.Practices.EnterpriseLibrary.Common.dll

If you con't have an app.config file, add one now (you could have this done for you later, but let's keep it simple).

Now open up "Enterprise Library Configuration", a utility installed along with the Enterprise Library that can be found off the Start menu....


Do File/Open... and point to the app.config file in your app.

Right-click the name of your app, select New... and then "Exception Handling Application Block".

Right-click the newly created tree node (it will have a red stop sign with a white X in it) and select New..Exception Policy. Name this "Global Policy".

Save it.

Now, back in Visual Studio 2005, your Try Catch blocks should be changed to the following pattern:

Try
'for testing: Throw New Exception("This is a sample exception.")
'put your real code here


Catch ex As Exception
Dim rethrow As Boolean = Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(ex, "Global Policy")
If rethrow Then
Throw
End If
End Try


You should now be able to compile and start playing with the advanced features of the Enterprise Library 3.0 version of the old ApplicationBlocks.

Also see: C# Tutorial.

No comments: