5

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) ? _container.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 ?

1
  • Did you ever find a solution to this problem, I'm getting the same error. Thanks Commented Nov 28, 2011 at 22:34

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

DanH is correct, there is no need to register anything MVC can instantiate itself. This would include the controllers.
2

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.

2 Comments

GetService method returned null when MVC requested HomeController, but then i got an exception "[MissingMethodException: No parameterless constructor defined for this object.]" and "[InvalidOperationException: An error occurred when trying to create a controller of type 'Website.Controllers.AccountController'. Make sure that the controller has a parameterless public constructor.]". Maybe i should to rewrite some class for this ?
try to add container.RegisterType<AccoutController, AccountController>() to Application_Start
1

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/

Comments

0

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.