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