I’d like to use Unity as an IoC container for an ASP.NET MVC 3 app but am having trouble with my UnityDependecyResolver class. It currently looks like this (copied from somewhere on the web as I don’t think Unity itself comes with this):
public class UnityDependencyResolver : IDependencyResolver
{
readonly IUnityContainer _container;
public UnityDependencyResolver(IUnityContainer container)
{
this._container = container;
}
public object GetService(Type serviceType)
{
try
{
return _container.Resolve(serviceType);
}
catch
{
return null;
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return _container.ResolveAll(serviceType);
}
catch
{
return new List<object>();
}
}
}
However, I get this runtime error when trying to visit any controller:
The IControllerFactory ‘System.Web.Mvc.DefaultControllerFactory’ did not return a controller for the name ‘Account’.
This StructureMap article suggests that I should amend the GetService method, however, I’m quite new to both MVC and Unity and I’m not sure how exactly should it look like.
Have a look at the Unity.MVC project on codeplex.