Unity IoC does not inject dependency into Web API Controller

前端 未结 4 2035
北荒
北荒 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:16

    • Install Unity.mvc5 and Unity.WebApi in a project through nuget package
    • Specify fullnamespace as per below line

    Example:

    public static class UnityConfig
    {
        public static void RegisterComponents()
        {
            var container = new UnityContainer();
    
            // register all your components with the container here
            // it is NOT necessary to register your controllers
    
            // e.g. container.RegisterType();
    
            DependencyResolver.SetResolver(new Unity.Mvc5.UnityDependencyResolver(container));
    
            GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
        }
    }
    

提交回复
热议问题