0
votes

At first I started the project with asp.net mvc and unity.mvc for DI, then wanted to add web api to the same project and installed unity.webapi but with that now unity dependency injection is unable to inject service instances to the ApiControllers however the controller class are working.

UnityConfig.cs

public static class UnityConfig
    {
        private static Lazy<IUnityContainer> container =
          new Lazy<IUnityContainer>(() =>
          {
              var container = new UnityContainer();
              RegisterTypes(container);
              return container;
          });

        public static IUnityContainer Container => container.Value;

        public static void RegisterTypes(IUnityContainer container)
        {
            container.RegisterType<IEmployeeService, EmployeeService>();
            container.RegisterType<IRepository<Employee>, Repository<Employee>>();
        }
    }

ApiController.cs

public class EmployeeApiController : ApiController
    {
        private readonly IEmployeeService _employeeService;

        public EmployeeApiController(IEmployeeService employeeService)
        {
            _employeeService = employeeService;
        }

        public EmployeeApiController(){}



        // GET: api/EmployeeApi
        public IEnumerable<Employee> Get()
        {
            var a = _employeeService.GetAll();
            return a;
        }
}

Global.asax.cs

public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            GlobalConfiguration.Configure(WebApiConfig.Register);

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

On the get action of apicontroller the IService throughs a null pointer exception.

2
Found the solution: added the following line on the UnityConfig.cs: GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container); - Peeyalk Khondokar

2 Answers

2
votes

Web API defines the following interfaces for resolving dependencies:

public interface IDependencyResolver : IDependencyScope, IDisposable
{
    IDependencyScope BeginScope();
}

public interface IDependencyScope : IDisposable
{
    object GetService(Type serviceType);
    IEnumerable<object> GetServices(Type serviceType);
}

As you pointed out in your comment, the Unity.WebApi nuget package provides this implementation which you can just register in the application startup.

For full reference:

public static class UnityConfig
{
    public static void RegisterComponents()
    {
        var container = new UnityContainer();
        GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
    }
}

public class WebApiApplication : System.Web.HttpApplication
{
  protected void Application_Start()
  {
    UnityConfig.RegisterComponents();
  }           
}  
-1
votes

Found the solution: added the following line on the UnityConfig.cs:

public static void RegisterTypes(IUnityContainer container)
{
    //Add this line
     GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);

       container.RegisterType<IEmployeeService, EmployeeService>();
       container.RegisterType<IRepository<Employee>, Repository<Employee>>();
}