1
votes

First time using MS Unity. I have a controller with the following constructor:

 protected IAdministrationService AdministrationService { get; set; }

 public GenerateCacheController(IAdministrationService administrationService)
 {
    AdministrationService = administrationService;
 }

I get the following error when trying to run the project:

Make sure that the controller has a parameterless public constructor.

In my Bootstrpper.cs file I have the following in the RegisterTypes method:

container.RegisterType<GenerateCacheController>();

I still get the error. Am I missing anything else? I'm using ASP.NET MVC 5 and Unity 3.

Here's my Boostrapper.cs file:

 public static class Bootstrapper
{
    public static IUnityContainer Initialise()
    {
        var container = BuildUnityContainer();
        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
        return container;
    }

    private static IUnityContainer BuildUnityContainer()
    {
        var container = new UnityContainer();
        RegisterTypes(container);
        return container;
    }

    public static void RegisterTypes(IUnityContainer container)
    {
        container.RegisterInstance(container);
        var im = new InjectionMember[0];
        container.RegisterType<IAdministrationService, AdministrationService>("AdministrationService", im);
        container.RegisterType<ILookupMapper, LookupMapper>("LookupMapper", im);
        container.RegisterType<IEmailService, EmailService>("EmailService", im);

        container.RegisterType<GenerateCacheController>();

        var provider = new UnityServiceLocator(container);
        ServiceLocator.SetLocatorProvider(() => provider);
    }
}

Abbreviated version of the AdministrationService class:

 public class AdministrationService : IAdministrationService
{
    protected ILookupMapper LookupMapper { get; set; }
    protected IEmailService EmailService { get; set; }

    public AdministrationService(ILookupMapper lookupMapper, IEmailService emailService)
    {
        LookupMapper = lookupMapper;
        EmailService = emailService;
    }
}
1
add a parameterless public constructor ? - Selman Genç
@Selman22 - that is not the right answer. The error is occurring because MS unity is not "injecting" anything into the constructor, which is what it should be doing (and removing the need for a parameterless constructor). However, I only have experience in Ninject and not Unity, so I cannot tell what is not configured correctly for the OP - Tommy
Show your AdministrationService class. - haim770
do you use the same container instance for resolving? - thumbmunkeys
When is Bootstrapper.Initialise() getting called? That should either be in the Application_Start (above everything else) or wired-up using WebActivatorEx PreApplicationStart. - Brad Christie

1 Answers

0
votes

Found the issue.

I commented out the line:

 var im = new InjectionMember[0];
 container.RegisterType<IAdministrationService, AdministrationService>("AdministrationService", im);

and added:

container.RegisterType<IAdministrationService, AdministrationService>();

And that worked because the previous developers were doing something like this:

private IUnityContainer Container { get; set; }

public AdministrationService()
{
    Container = Microsoft.Practices.ServiceLocation.ServiceLocator.Current.GetInstance<IUnityContainer>();
}

instead of

protected ILookupMapper LookupMapper { get; set; }
protected IEmailService EmailService { get; set; }

public AdministrationService(ILookupMapper lookupMapper, IEmailService emailService)
{
    LookupMapper = lookupMapper;
    EmailService = emailService;
}

I have to go back to their way to not break existing code. I'll get around to refactoring one day.