Unity IoC does not inject dependency into Web API Controller

前端 未结 4 2005
北荒
北荒 2020-12-18 18:50

I\'m very new to using Unity, but my problem is that whenever I call my web service, I get an exception stating that

\"Make sure that the controller has a parameterl

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-18 19:27

    I know this is an old post but since I just ran into the same problem, the solution was to simply pass the config object to the Unity registration function and use the DependencyResolver from that instance:

    EDIT: Just noted that the accepted answer does exacly this... Sorry for the spam ;-)

    public static class UnityConfig
    {
        public static void Register(HttpConfiguration config)
        {
            var container = new UnityContainer();
    
            // register your services here...
    
            // replace...
            /* GlobalConfiguration.Configuration.DependencyResolver = new      Unity.WebApi.UnityDependencyResolver(container); */
    
            // with this:
            config.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
        }
    }
    

提交回复
热议问题