Corey Witherow
Corey Witherow

Reputation: 2472

Unity -The current type is an interface and cannot be constructed. Are you missing a type mapping?

I'm using Unity Dependency Injection and am having troubles with it. I have registered my Interfaces and classes like so:

container.RegisterType<IUserService, UserService>();

where the container is an IUnityContainer.

In my controller I have a constructor setup to take in an IUserService parameter like so:

private readonly IUserService _service;

    public DependencyController(IUserService service)
    {
        _service = service;
    }

When the link on the page that is directed to the index of this controller is hit, I get the following error:

"The current type, Converge.Service.Interfaces.IUserService, is an interface and cannot be constructed. Are you missing a type mapping?". Not sure what I might be missing. Any help is greatly appreciated? If you need more info let me know.

UPDATE here is the UnityConfig class

public class UnityConfig
{
    private static readonly Lazy<IUnityContainer> Container = new Lazy<IUnityContainer>(() =>
    {
        var container = new UnityContainer();

        RegisterTypes(container);

        return container;
    });

    public static IUnityContainer GetConfiguredContainer()
    {
        return Container.Value;
    }

    private static void RegisterTypes(IUnityContainer container)
    {
        new ServiceInjection()
            .Inject(container);
    }
}

public class ServiceInjection
    {
        public void Inject(IUnityContainer container)
        {
            container.RegisterType<IUserService, UserService>();
        }
    }

Upvotes: 1

Views: 5031

Answers (2)

selegnasol
selegnasol

Reputation: 11

I know this is old, but in case someone else comes across this -- I got the same error when I added a service and a manager (and interfaces for them), but didn't register these interfaces in my Dependency Injection Configuration. If you get this error, make sure to check that your new interfaces are actually being injected!

Upvotes: 1

Corey Witherow
Corey Witherow

Reputation: 2472

Ok so after a little searching and creating other projects I figured out what was happening. Just a stupid mistake. My ServiceInjection class was in a separate dll. When I added the Unity DI through the package manager, it added a App_Start folder in the main project and in the Injection dll. I forgot to remove the App_Start folder and contents from the ServiceInjection dll. Once that folder was removed, all worked as it should.

Upvotes: 0

Related Questions