0

Unity config newbie, I am trying to implement this in my project. However, I am stuck.

I receive the following error: The current type, System.Web.Mvc.IControllerFactory, is an interface and cannot be constructed. Are you missing a type mapping?

ContainerClass

public class ContainerBootstrapper
    {
        public static void ConfigureUnityContainer()
        {
            //simple registeration
            container.RegisterType<IProduct, ProductHelper>(); //maps interface to a concrete type

            System.Web.Mvc.DependencyResolver.SetResolver(new MyProjectControllerDependency(container));

        }

    }

DependencyResolver

public class MyProjectControllerDependency : IDependencyResolver
{
private IUnityContainer _container;

public MyProjectControllerDependency(IUnityContainer container)
{
    this._container = container;
}

public object GetService(Type serviceType)
{
    return _container.Resolve(serviceType);
}

public IEnumerable<object> GetServices(Type serviceType)
{
    return _container.ResolveAll(serviceType);
}

Controller:

public class ProductController : ApiController
{
    private readonly IProduct _iproduct;

    public ProductController(IProduct product)
    {
        this._iproduct = product;
    }
 //controller methods
}

Interface

public interface IProduct
{
    List<ProductViewModel> GetProductByBarcode(string value);
    string GetProductPrice(string value);
}

Helper

public class ProductHelper : IProduct
    {
        //private readonly IProduct _iproduct;

        //public ProductHelper(IProduct iproduct)
        //{
        //    this._iproduct = iproduct;
        //}

        public List<ProductViewModel> GetProductByBarcode(string value)
        {
            throw new NotImplementedException();
        }

        public string GetProductPrice(string value)
        {
            throw new NotImplementedException();
        }
}

I don't understand, what I am missing? Can anyone point me in the right direction.

6
  • Are you using Unity.Mvc? Commented May 1, 2017 at 15:03
  • I installed unity via nuget, I am not sure. @ChetanRanpariya Commented May 1, 2017 at 15:40
  • 1
    For MVC and WebAPI project, Unity.MVC is required. You won't even need to create custom DependencyResolver. Install Unity.Mvc and look for examples in google. It's easy to configure and plug-in. Commented May 1, 2017 at 15:42
  • I will try this, and get back to you Commented May 1, 2017 at 16:02
  • 1
    I am glad that it helped you to resolve the issue. I just posted by answer below. Thanks. Commented May 1, 2017 at 16:17

1 Answer 1

1

For ASP.NET MVC and WebApi projects using only Unity is not sufficient. For these projects you also need to install Unity.MVC from nuget.

This is provides out of the box ready to use classes. You just need to register the dependencies in the UnityContainer and done.

Once you install Unity.MVC, it creates classes UnityMvcActivator and UnityConfig. UnityConfig class has implementation of initializing UnityContainer. All you need to do it register dependencies in RegisterTypes method.

public static void RegisterTypes(IUnityContainer container)
{
    container.RegisterType<IBaseClass, BaseClass>(); // Registering types
    container.LoadConfiguration();
}

You don't need to create any custom type or implementation unless and until you have completely different requirement.

This should help you resolve your issue.

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

1 Comment

I will accept this as it made me realise i need to implement unity for web api not not mvc for my purposes as I am only using web api.

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.