我目前无法让Ninject将依赖项注入我的API控制器。我正在使用一个带有MVC 5,Web API 2.1和SignalR 2的Web项目。我使用的是Web API的5.1.2版,Ninject的3.2.2版和Ninject.Web.WebApi的3.2.1版。主机商。每次我对API提出请求时,都会收到此错误:
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()
这是它试图实例化的控制器:
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;
}
这是我的Startup.cs
文件:
public class Startup { public static IKernel Kernel {get;私人集; }
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>();
}
}
这些模块包含所有必需的依赖项。我已经证实他们都受到了正确的约束。有谁知道为什么我仍然会收到此错误?有什么办法可以解决它吗?
答案 0 :(得分:4)
我找到了解决方案。对我而言,这只是一个愚蠢的错误。我的依赖项绑定未正确配置。如果只有我得到了更有帮助的例外......