我正在开发一个ASP.NET MVC应用程序。在我的应用程序中,我需要提供REST API。所以我将Web Api 2添加到我现有的MVC应用程序中。在我添加Web API 2之前,我使用ninject进行依赖注入。我是通过Nuget Package安装的。整个网站已经开发和运作。但是当我将Web Api 2添加到我的项目时,问题就出现了。 Ninject for MVC不能与Web Api一起使用。所以我安装了Ninject for Web Api 2.所以我的NinjectWebCommon类在安装后已经改变了。
这是我在App_Start文件夹中的NinjectWebCommon文件
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(AyarDirectory.Web.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(AyarDirectory.Web.App_Start.NinjectWebCommon), "Stop")]
namespace AyarDirectory.Web.App_Start
{
using System;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
System.Web.Mvc.DependencyResolver.SetResolver(new AyarDirectory.Web.Infrastructure.NinjectDependencyResolver(kernel));
}
}
}
在我为web api安装ninject后,我只更改了那个地方。然后我运行我的应用程序。网站工作正常。网站仍在运作。但Web Api无效。
这是我的解析器类
namespace AyarDirectory.Web.Infrastructure
{
public class NinjectDependencyResolver : IDependencyResolver
{
private IKernel kernel;
public NinjectDependencyResolver(IKernel kernelParam)
{
kernel = kernelParam;
AddBindings();
}
public object GetService(Type serviceType)
{
return kernel.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return kernel.GetAll(serviceType);
}
private void AddBindings()
{
kernel.Bind<ICategoryRepo>().To<CategoryRepo>();
}
}
}
我在这样的Api Controller中进行依赖注入
public class RegionsController : ApiController
{
private IRegionRepo regionRepo;
private RegionsController(IRegionRepo regionParam)
{
this.regionRepo = regionParam;
}
. . . }
当我访问api控制器的一个动作时,它正在给我 跟随错误。
{"Message":"An error has occurred.","ExceptionMessage":"An error occurred when trying to create a controller of type 'RegionsController'. Make sure that the controller has a parameterless public constructor.","ExceptionType":"System.InvalidOperationException","StackTrace":" at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Type 'AyarDirectory.Web.Controllers.Api.RegionsController' does not have a default constructor","ExceptionType":"System.ArgumentException","StackTrace":" at System.Linq.Expressions.Expression.New(Type type)\r\n at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"}}
那么,为什么它不起作用?我的代码有什么缺失或错误?请问我该如何解决?我如何使用Ninject for Web Api 2?我正在使用MVC 5.
答案 0 :(得分:1)
下面再做两个步骤:
install-package Ninject.Web.WebApi
GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
返回内核;