ASP.NET MVC 3, Unity 2.0, DependencyResolver, Exception Handling and Logging Application Blocks

Topics: Exception Handling Application Block, General discussion, Logging Application Block
Mar 18, 2011 at 7:55 AM

On my ASP.NET MVC 3 app, I have implemented a UnityDependencyResolver class that implements the IDependencyResolver interface. Inside my global.asx, I have the following method being called inside Application_Start to register and map my custom types using a Unity container for Dependency Injection:

        private void RegisterDependencyResolver()
            var container = new UnityContainer()
                .RegisterType<IAccountRepository, AccountRepository>()
                .RegisterType<IAccountService, AccountService>()
                .RegisterType<IProviderRepository, ProviderRepository>()
                .RegisterType<IProviderService, ProviderService>();
            DependencyResolver.SetResolver(new UnityDependencyResolver(container));

Inside the Controllers that have dependencies with my custom types, I defined a constructor where I pass the interface(s) of these dependent types.

I would like to use the Exception Management and Logging Application Blocks on my MVC 3 app. The documentation does not provide guidance on what is a good way to do this on an MVC 3 app. Any suggestions?

Mar 18, 2011 at 8:09 AM

Ok, I'm trying to think first what's the connection between your RegisterDependencyResolver method and your concern on how to implement exception handling and logging in your MVC app.   Am I missing something?  


Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.

Mar 18, 2011 at 8:26 AM

The connection is how do I inject the Enteprise Library types into my Controllers that will be using Exception Handling and Logging in the same way I am injecting my custom dependent types via the DepedencyResolver of MVC3 I described above?

Mar 18, 2011 at 8:38 AM

You just need to add the EnterpriseLibraryCoreExtension to the unity container.  This extension automatically adds all entlib object type mappings included in your configuration file.  So if your controller needs an instance of  LogWriter, you can make it a parameter to its constructor and and when you create that controller out of the unity container where you add the EnterpriseLibraryCoreExtension, it will be able to inject the LogWriter object you need.


Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.