开发者

MVC 3 beta + Dependency Resolver + Unity = got problem

I'm tried to use Dependency Resolver with Unity, and got some problem when my Controller creating. Here example of controller:

public class AccountController : Controller
{
    private readonly ICourseService _courseService;

    public AccountController(ICourseService courseService)
    {
         _courseService = courseService;
    }
}

But, when Controller try to create - i got an exception "No parameterless constructor defined for this object." I even try to add default constructor for this controller, but courseService didn't create. Also try to add property with [Dependency] attribute - nothing happened. Here is Dependency Resolver class:

public class UnityDependencyResolver : IDependencyResolver
{
    private readonly IUnityContainer _container;
    public UnityDependencyResolver(IUnityContainer container)
    {
        _container = container;
    }

    public object GetService(Type serviceType)
    {
        return _container.IsRegistered(serviceType) ? _contai开发者_运维知识库ner.Resolve(serviceType) : null;
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return _container.IsRegistered(serviceType) ? _container.ResolveAll(serviceType) : new List<object>();
    }
}

and Global.asax.cs:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

        var container = new UnityContainer();
        container.RegisterType<ICourseService, CourseService>();

        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    }

Can anyone help me ?


Old post but these other answers are flat out wrong. Had this problem myself today, and you definitely do not need to register the controllers or a controller factory. This message just means that something in the dependency hierarchy for the controller is not registered. E.g. If your controller requires IService in it's constructor and IService requires IRepository in its constructor, but you forgot to register IRepository you will get this error.


Your IDependencyResolver implementation requires that AccountController is registered. Try adding this registation otherwise it will return null and MVC will try to create the controller with the Activator which requires a parameterless ctor.


The code above will break if your controllers have dependencies on non registered types that are concrete. You should modify the GetService() to the following:

 public object GetService(Type serviceType){
    if (!container.IsRegistered(serviceType)){
      if (serviceType.IsAbstract || serviceType.IsInterface){
        return null;
      }
    }
    return container.Resolve(serviceType);
  }

That way if say CourseService has a dependency on the concrete CourseRepository, the container will instantiate it.

I'm with DanH in the assumption that somewhere in the hierarchy there is a missing type registration, or a dependency on a concrete type that is not getting instatiated.

I had this same issue, and found the following article helpful:

http://xhalent.wordpress.com/2011/01/17/using-unity-as-a-dependency-resolver-asp-net-mvc-3/


Your problem is that MVC is still attempting to instantiate the controller with the default controller factory, which requires a parameter-less constructor, and has no knowledge of your Unity container or how to resolve the ICourseService dependency.

You need to build a custom controller factory to take advantage of Unity (specifically one that overrides GetControllerInstance(Type type) - there should be plenty of documentation and samples around for this; it's a straightforward class - and then register it in Application_Start as follows:

ControllerBuilder.Current.SetControllerFactory (new MyUnityControllerFactory(container));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜