locked
Web API Dependency Injection with Unity RRS feed

  • Question

  • User1548699010 posted

    I have a project using asp.net web api 2 with IoC of Unity, but I have problems here:

    By following the link here: http://www.asp.net/web-api/overview/advanced/dependency-injection, I setup a test service and test controller, which work ok. The code is as follows:

    TestService:

    public class TestService : ITestService
    {

              public string GetQuote()
              
                 return "6.9999";
              }
    }

    ITestService:

    public interface ITestService
    {
              string GetQuote();
    }

    Controller:

    [RoutePrefix("api/test")]
    public class TestController : BaseApiController
    {
           private readonly ITestService _testService;

           public TestController(ITestService testService)
           {
                _testService = testService;
           }   

           [Route("rate2")]
           public string Get()
           {

                 eturn _testService.GetQuote();

           }

    }

    My mapping in WebApiConfig.cs

    public static void Register(HttpConfiguration config)
    {

             //DI Configuration
             var container = new UnityContainer();
             container.RegisterType<ITestService, TestService>(); 
             

             config.DependencyResolver = new UnityResolver(container);

    //Other config

    }

    By using postman, it returns "6.9999". It seems working.

    However, when I added DI implementation to the TestService to inject repository (I am using generic repository pattern) as follows:

    I added this to TestService.cs:

    private readonly IRepository<UserProfile> _userProfileRepoistory;

    public TestService(IRepository<UserProfile> userProfileRepoistory)
    {
            _userProfileRepoistory = userProfileRepoistory;
    }

    Because I need to get data from database through repository.

    Of course, I add the following to the WebApiConfig.cs

    container.RegisterType(typeof(IRepository<>), typeof(Repository<>));

    container.RegisterType<IRepository<UserProfile>, Repository<UserProfile>>();

    it stops working with error:

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

    I searched for couple of day, no luck. Please help.

    (I tried to use Ninject, but the same problem)

    Bob

    Tuesday, May 3, 2016 7:29 PM

All replies

  • User1548699010 posted

    Update: I tried not using generic repository, it seems working. I guess it does not like generic repository? is there anyway to get around this?

    Tuesday, May 3, 2016 10:33 PM
  • User36583972 posted

    Hi byuan,

    From your error message, you can try the following methods.

    1: Resolve your controller using your IoC

    You can try to resolve your controller using your IoC. In your WebAPI Register method, you can set the DependencyResolver to your IoC container.

    config.DependencyResolver = new UnityDependencyResolver(UnityHelpers.GetConfiguredContainer());
    

    More detailed information you can refer the following post.
    Web API 2.1 Dependency Injection:
    http://forums.asp.net/t/1982840.aspx?Web+API+2+1+Dependency+Injection

    2: Add a public parameterless constructor

    public class AnalyticController : ApiController
    {
       public AnalyticController()
       {
       }
    }

    Best Regards,

    Yohann Lu

    Wednesday, May 4, 2016 7:16 AM
  • User1548699010 posted

    Thanks Yohann, but I cannot get UnityHelpers namespace, even I installed it with Nuget(UnityHelpers v.1.0.9), but it seems not it is still not found. by the way, did you meant to add this line to startup.cs, but in my WebApiConfig,cs, I already have a line similiar to this: config.DependencyResolver = new UnityDependencyResolver(container);

    Bob

    Wednesday, May 4, 2016 9:05 PM
  • User36583972 posted

    Hi byuan,

    I found your code is incomplete. Please show the complete code to us. Which code cause the error should highlighted.

    Best Regards,

    Yohann Lu

    Monday, May 9, 2016 11:06 AM