1

I am using Asp.Net MVC 5 and am trying to wire up my dependencies for a web api controller but it doesnt work and says the controller needs a parameter less constructor.

I have updated unity container to v4 which meant updating some other references, namely unity.abstractions is also v4.

I am using unity.mvc not unity.mvc5.

In UnityConfig I have

public static class UnityConfig
{
    private static readonly Lazy<IUnityContainer> Container = new Lazy<IUnityContainer>(InitialiseContainer, LazyThreadSafetyMode.ExecutionAndPublication); 

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

    private static IUnityContainer InitialiseContainer()
    {
        var container = new UnityContainer();        

        //DependencyResolver.SetResolver(new Unity.Mvc5.UnityDependencyResolver(container));
        //GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);                                             

        // Auth
        container.RegisterType<IAuthenticationManager>(new InjectionFactory(c => HttpContext.Current.GetOwinContext().Authentication));            
        container.RegisterType<ApplicationUserManager>(new InjectionFactory(c => HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>()));
        container.RegisterType<IUserStore<LiApplicationUser>> (new InjectionFactory(c => new UserStore<LiApplicationUser>(new ApplicationIdentityDbContext())));
        // Repository
        container.RegisterType<LeisureInsureEntities>(new InjectionFactory(c => ContextFactory.Context()));
        container.RegisterType<ICar, Volvo>();

        DependencyResolver.SetResolver(new UnityDependencyResolver(container));

        return container;
    }       
}

I commented out the unity.mvc5 code is no longer needed, so its removed. That seemed to require unity.abstractions v2, and doesn't accept 4.

As I am trying to inject into a webapi controller do I also need Unity.WebAPI? that gives the same problem as Unity.Mvc5 and is developed by the same person, ie it doesnt like my later version of unity.abstractions.

In my webapi controller I am seeing if I can wire up Volvo as a test

public class StripeController : ApiController
{               

    private readonly IEndpointInstance _endpoint;
    private readonly ICar car;

    //public StripeController(IEndpointInstance endpoint)
    //{
    //    _endpoint = endpoint;
    //}        

    public StripeController(ICar newcar)
    {
        car = newcar;
    }

Thanks

1 Answers1

3

You will need the Unity.AspNet.WebApi adapter to configure your ApiControllers.

Careful you don't mix up packages. Some of those are not by the same project owner (Unity.Mvc5, Unity.WebAPI). I haven't used those so I can't comment on its suitability.

I have a example https://github.com/jasenhk/MovieStar from another answer that uses Unity v5.10 and Unity.MVC as well as Unity.AspNet.WebApi.

Here is the packages.config

<package id="Unity" version="5.10.2" targetFramework="net461" />
<package id="Unity.Abstractions" version="4.1.2" targetFramework="net461" />
<package id="Unity.AspNet.WebApi" version="5.10.0" targetFramework="net461" />
<package id="Unity.Container" version="5.10.2" targetFramework="net461" />
<package id="Unity.Mvc" version="5.10.0" targetFramework="net461" />
Jasen
  • 14,030
  • 3
  • 51
  • 68