使用ASP.Net MVC 4 Web API和Ninject.Web.WebApi发布问题
我正在尝试将新的ASP.Net MVC 4 Web API项目模板与Ninject一起使用,但是在以下错误中遇到了障碍:
程序集’Ninject.Web.WebApi.Filter.DefaultFilterProvider’中的方法’GetFilters’来自程序集’Ninject.Web.WebApi,Version = 3.0.0.0,Culture = neutral,PublicKeyToken = c7192dc5380945e7’没有实现。
我正在使用ASP.Net MVC 4 – > Web API模板在Visual Studio 2010中创建一个全新的项目,我正在使用最新的Ninject NuGet包:
- Ninject 3.0.1.10
- Ninject.Web.Common 3.0.0.7
- Ninject.Web.WebApi 3.0.0.2
我尝试过在这个问题中提出的解决方案,但是我没有运气 – 如果我删除对Ninject.Web.WebApi的引用,那么MVC永远不会使用Ninject。 我也注意到他们提到了Ninject.MVC3但是我使用的是新的Ninject.WebApi插件。
我正在使用NinjectWebCommon.cs中的默认绑定代码,该代码是在NuGet安装期间创建的,并尝试在RegisterServices()中注册一个简单服务
[assembly: WebActivator.PreApplicationStartMethod(typeof(mkts.web.App_Start.NinjectWebCommon), "Start")] [assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(mkts.web.App_Start.NinjectWebCommon), "Stop")] namespace mkts.web.App_Start { using System; using System.Web; using Microsoft.Web.Infrastructure.DynamicModuleHelper; using Ninject; using Ninject.Web.Common; using mkts.service; public static class NinjectWebCommon { private static readonly Bootstrapper bootstrapper = new Bootstrapper(); /// /// Starts the application /// public static void Start() { DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); bootstrapper.Initialize(CreateKernel); } /// /// Stops the application. /// public static void Stop() { bootstrapper.ShutDown(); } /// /// Creates the kernel that will manage your application. /// /// The created kernel. private static IKernel CreateKernel() { var kernel = new StandardKernel(); kernel.Bind<Func>().ToMethod(ctx => () => new Bootstrapper().Kernel); kernel.Bind().To(); RegisterServices(kernel); return kernel; } /// /// Load your modules or register your services here! /// /// The kernel. private static void RegisterServices(IKernel kernel) { //Test binding kernel.Bind().To(); } } }
我的控制器:
namespace mkts.web.Controllers { public class HomeController : Controller { private readonly IStudentService studentService; public HomeController(IStudentService studentService) { this.studentService = studentService; } public ActionResult Index() { return View(); } } }
非常感谢您提供任何帮助。
Ninject.WebApi是针对Beta编写的,现已弃用。
删除它,安装Ninject.MVC3。
现在您将需要一个自制的IDependencyResolver和IDependencyScope。 我在这里发布了一个演练 – http://www.strathweb.com/2012/05/using-ninject-with-the-latest-asp-net-web-api-source/
- 数据注释未显示MVC 4中的部分实体类
- 更新PartialView mvc 4
- 不支持每种类型的多个对象集。 对象集IdentityUsers’和用户都可以包含类型的实例
- 将图表绘制到ASP.NET MVC 4(Razor,C#)网站
- 在mvc4中的Global.asax中的会话超时重定向
- 没有类型为’IEnumerable ‘的ViewData项具有关键国家/地区
- 动态扩展应用程序的function?
- 如何处理WebApi错误404
- 安装文件的位置 – Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework \ .NETFramework \ v4.5