Unity IoC does not inject dependency into Web API Controller

前端 未结 4 2003
北荒
北荒 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<ITestService, TestService>();
    
            DependencyResolver.SetResolver(new Unity.Mvc5.UnityDependencyResolver(container));
    
            GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
        }
    }
    
    0 讨论(0)
  • 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);
        }
    }
    
    0 讨论(0)
  • 2020-12-18 19:28

    Your Unity container is probably getting disposed because it's only defined in the scope of your WebApiConfig.Register() method. If you define your container as a member of Global, which will keep your container around for the lifetime of the app, it should work.

    Edit: also don't forget to dispose the container on Application_End.

    0 讨论(0)
  • 2020-12-18 19:34

    So, after hours of banging my head against a wall, I've found that this wasn't working because I had an OWIN/Katana Startup.cs class in my project. Now I don't know exactly what's going on here, so any more information would be great.

    Basically, since I was using OWIN/Katana, I had a Startup.cs file that created a new HttpConfiguration object and configured it, similar to how it's done in the WebApiConfig.cs class.

    private void ConfigureWebApi(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();
        var container = new UnityContainer();
        container.RegisterType<IValidator, Validator>();
        config.DependencyResolver = new UnityDependencyResolver(container);
    
        var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter().First();
        jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    }
    

    It looks like the code first runs through the WebApiConfig's Register function, and THEN overrides that HttpConfiguration object with the one generated in the Startup.cs file. I had to move my container setup stuff here in order for it to work.

    Sorry that I didn't bring up the OWIN stuff before. This is fairly new to me and I didn't realize it was relevant. Hopefully this saves someone else the pain that I've just been through.

    0 讨论(0)
提交回复
热议问题