1
votes

I'm currently having trouble getting Ninject to inject dependencies into my API controllers. I am using a Web project with MVC 5, Web API 2.1 and SignalR 2. I'm using version 5.1.2 of Web API, version 3.2.2 of Ninject, and version 3.2.1 of Ninject.Web.WebApi.Webhost. Every time I make a request against the API, I get this error:

    System.InvalidOperationException: An error occurred when trying to create a controller of type 'AccountsController'. Make sure that the controller has a parameterless public constructor. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at Ninject.Planning.Bindings.BindingConfiguration.GetProvider(IContext context)
   at Ninject.Planning.Bindings.Binding.GetProvider(IContext context)
   at Ninject.Activation.Context.GetProvider()
   at Ninject.Activation.Context.ResolveInternal(Object scope)
   at Ninject.Activation.Context.Resolve()
   at Ninject.KernelBase.<>c__DisplayClass15.<Resolve>b__f(IBinding binding)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent)
   at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent)
   at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)
   at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target)
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
   at Ninject.Activation.Context.ResolveInternal(Object scope)
   at Ninject.Activation.Context.Resolve()
   at Ninject.KernelBase.<>c__DisplayClass15.<Resolve>b__f(IBinding binding)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent)
   at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent)
   at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)
   at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target)
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
   at Ninject.Activation.Context.ResolveInternal(Object scope)
   at Ninject.Activation.Context.Resolve()
   at Ninject.KernelBase.<>c__DisplayClass15.<Resolve>b__f(IBinding binding)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at Ninject.Web.WebApi.NinjectDependencyScope.GetService(Type serviceType)
   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)
   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
   --- End of inner exception stack trace ---
   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
   at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)
   at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at System.Web.Http.HttpServer.<SendAsync>d__0.MoveNext()

Here is the controller it is trying to instantiate:

public class AccountsController : ApiControllerBase
{
    private IAuthenticationService _auth;
    private IConfigurationManager _config;
    private static readonly ILog LOG = LogManager.GetLogger(typeof(AccountsController));

    public AccountsController(IAuthorizationService authService, IAuthenticationService auth, IConfigurationManager config)
        : base(authService)
    {
        _auth = auth;
        _config = config;
    }

And here is my Startup.cs file:

public class Startup { public static IKernel Kernel { get; private set; }

    static Startup()
    {
        Kernel = new StandardKernel();
        RegisterServices(Kernel);
    }
    public void Configuration(IAppBuilder app)
    {
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888



        var webApiConfig = ConfigureWebApi(Kernel);

        app.UseWebApi(webApiConfig);
    }

    public static HttpConfiguration ConfigureWebApi(IKernel kernel)
    {
        var config = new HttpConfiguration();
        config.DependencyResolver = new Ninject.Web.WebApi.NinjectDependencyResolver(kernel);

        //Formatting
        config.Formatters.Remove(config.Formatters.XmlFormatter);
        config.Formatters.Remove(config.Formatters.JsonFormatter);
        config.Formatters.Add(new JsonMediaTypeFormatter()
        {
            SerializerSettings = JsonSettings.SETTINGS,
            Indent = true,
        });

        //Error handling
        config.Services.Add(typeof(IExceptionLogger), new Log4NetExceptionLogger());
        config.Services.Replace(typeof(IExceptionHandler), new EntityErrorExceptionHandler());

        //Routing
        config.MapHttpAttributeRoutes();
        config.EnsureInitialized();
        return config;
    }


    private static void RegisterServices(IKernel kernel)
    {
        kernel.Load<ConfigurationModule>();
        kernel.Load<ResourceModule>();
        kernel.Load<SecurityModule>();

        kernel.Bind<IEmailService>().To<Email.SendGridService>();


    }
}

Those modules contain all the necessary dependencies. I have verified that they are all getting bound properly. Does anyone know why I still get this error? Is there anything that can be done to fix it?

1

1 Answers

4
votes

I found the solution. It was simply a bone-headed mistake on my part. My dependency bindings were not configured correctly. If only I had been given a more helpful exception...