0

I have created an application using MVC5 with the onion architecture approach. The solution contains 3 projects (core, infrastructure, and UI). The UI contains both Web API controllers and MVC controllers. The issue I’m running into is dependency injection. I have installed Unity.MVC5 & Unity.WebApi. My UnityConfig.cs under App_Start Looks like this:

public static void RegisterComponents()
    {
        var container = new UnityContainer();

        container.RegisterType<IPricingService, PricingService>();

        GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
        GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
    }

My global.asax looks like this:

protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        System.Web.Http.GlobalConfiguration.Configure(WebApiConfig.Register);

        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        UnityConfig.RegisterComponents();

    }

To test out my controller, I defined my home controller like this:

private readonly IPricingService _pricingService;

    public HomeController(IPricingService PricingService)
    {
        this._pricingService = PricingService;
    }

When running home page I get No parameterless constructor defined for this object.

Now, moving to another test scenario, I created a web api controller and looks like this:

private readonly IPricingService _pricingService;

    public TestApiController(IPricingService PricingService)
    {
        this._pricingService = PricingService;
    }

Testing the web api generates this error:

An error occurred when trying to create a controller of type 'TextApiController'. Make sure that the controller has a parameterless public constructor.","exceptionType":"System.InvalidOperationException"

Not sure what I'm missing. Please advise.

1 Answer 1

1

You are supposed to inject the Unity.WebApi.DependencyResolver into the WebApi configuration not in GlobalConfiguration.Configuration.DependencyResolver.

public static void Register(HttpConfiguration config)
{
    var container = new UnityContainer();
    container.RegisterType<IProductRepository, ProductRepository>(new HierarchicalLifetimeManager());
    config.DependencyResolver = new UnityResolver(container);

    // Other Web API configuration not shown.
}

You also need to implement a child container in the BeginScope method as shown in this MSDN article.

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

3 Comments

Thanks for your comment, going to try that shortly.but what about the regular controller that generates the same error? Does this resolve both issues?
doesn't work. Can you provide an example that works with MVC5 + Web API? Thanks
it works with MVC5 + Web API. But there is no need to install Unity.WebApi.DependencyResolver if Unity.MVC5 is installed. Thanks @nightowl888

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.