Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make sure that the controller has a parameterless public constructor in Unity

I got this problem with the Controller:
An error occurred when trying to create a controller of type '*.WebMvc.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.

Find the solution for the ApiController, but didn't find anything about normal Controller.

Created new MVC 4 project from scratch.

HomeController.cs:

public class HomeController : Controller
{
    private IAccountingUow _uow;
    public HomeController(IAccountingUow uow)
    {
        _uow = uow;
    }

UnityDependencyResoler.cs:

public class UnityDependencyResolver : IDependencyResolver
{
    private IUnityContainer _container;
    public UnityDependencyResolver(IUnityContainer container)
    {
        _container = container;
        RegisterTypes();
    }
    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 null;
        }
    }

    private void RegisterTypes()
    {
        _container.RegisterType<IAccountingUow, AccountingUow>();

    }
}

Global.asax

    protected void Application_Start()
    {
        //Omitted
        DependencyResolver.SetResolver( new UnityDependencyResolver( new UnityContainer()));

    }

Debugged and found out, that there are even no attempts to resolve IAccountingUow.

What i'm doing wrong? Thinking about it whole day.

like image 201
Yaroslav Avatar asked Nov 12 '12 15:11

Yaroslav


People also ask

How to fix unity controller does not have a default constructor?

Make sure that the controller has a parameterless public constructor in Unity 3 Using Unity with Web Api 2 gives error does not have a default constructor 0 Injecting Dependency into Web API Controller 0 Setting proper Unity Container configuration for resolving interface-class in decorator pattern Related 1721 Calling the base constructor in C#

Why doesn't my controller have a parameterless constructor?

Your controller (as posted here) does have a parameterless public constructor. Your real controller probably has another constructor, not posted here, and as a result the default parameterless constructor is not generated.

How to register unity 2 controller with web API?

Make sure that the controller has a parameterless public constructor in Unity 2. Make sure to register Unity with Web API. Like so: public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Routes and other stuff here...

What happens if you create a parameterized constructor in a framework?

There is only one prerequisite, that is controller class must have a parameter less constructor. But if you need to pass objects as constructor parameters and for that reason if you create parameterized constructor then what will happen? Simply framework will fail to create those controllers object that have parameterized constructor.


2 Answers

Found where is the issue. Maybe some one will face the same. The problem is that Unity could not resolve the IAccountingUow, because of hierarchical dependancy on interfaces.

AccountingUow class has two contuctors

    public AccountingUow( IRepositoryProvider repositoryProvider)
    {
        Init(repositoryProvider);
    }
    public AccountingUow()
    {
        Init( new RepositoryProvider(new RepositoryFactories()) );
    }

Dependency Resolver is not smart enought to take the default parametless contructor. It tries to take interface dependant contructor and fails to resolve it, cause there are no rules for resolving it.

I commented out interface dependant constructor and everything worked fine.

I will post later resolver for the first contructor, maybe someone will use it.

like image 129
Yaroslav Avatar answered Oct 13 '22 12:10

Yaroslav


This can also be due to an exception in the parameter-injected constructor of the outer type that is being resolved. The dependencies of that type's constructor might be getting resolved successfully, but if there is an exception in the outer constructor, Unity will report it as "Type Test.Controllers.MyControllerWithInjectedDependencies does not have a default constructor".

like image 43
KrisG Avatar answered Oct 13 '22 14:10

KrisG