RegisterServices中的NinjectWebCommon绑定在WebApi中不适用于我
发布时间:2020-12-16 10:01:57 所属栏目:asp.Net 来源:网络整理
导读:我创建了一个新的ASP.NET Web API项目.然后我使用nuget拉Ninject.Web.Common,然后从 here下载并构建Ninject.Web.WebApi.将其包含在项目中.我添加了一个服务和注入通过构造函数,设置绑定(调试器显示代码实际上命中绑定)但仍然抛出此错误: Error activating I
我创建了一个新的ASP.NET Web API项目.然后我使用nuget拉Ninject.Web.Common,然后从
here下载并构建Ninject.Web.WebApi.将其包含在项目中.我添加了一个服务和注入通过构造函数,设置绑定(调试器显示代码实际上命中绑定)但仍然抛出此错误:
Error activating IValueService No matching bindings are available,and the type is not self-bindable. Activation path: 2) Injection of dependency IValueService into parameter valueService of constructor of type ValuesController 1) Request for ValuesController Suggestions: 1) Ensure that you have defined a binding for IValueService. 2) If the binding was defined in a module,ensure that the module has been loaded into the kernel. 3) Ensure you have not accidentally created more than one kernel. 4) If you are using constructor arguments,ensure that the parameter name matches the constructors parameter name. 5) If you are using automatic module loading,ensure the search path and filters are correct. 我项目中的图书馆: > Ninject.dll 所有ninject库都标记为版本3. 这是代码: [assembly: WebActivator.PreApplicationStartMethod(typeof(MvcApplication3.App_Start.NinjectWebCommon),"Start")] [assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(MvcApplication3.App_Start.NinjectWebCommon),"Stop")] namespace MvcApplication3.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(); kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel); kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); RegisterServices(kernel); return kernel; } /// <summary> /// Load your modules or register your services here! /// </summary> /// <param name="kernel">The kernel.</param> private static void RegisterServices(IKernel kernel) { kernel.Bind<IValueService>().To<ValueService>(); } } } 服务: public interface IValueService { List<string> GetStrings(); } public class ValueService : IValueService { public List<string> GetStrings() { return new List<string>{"test","test"}; } } 控制器: public class ValuesController : ApiController { private readonly IValueService valueService; // GET /api/values public ValuesController(IValueService valueService) { this.valueService = valueService; } } 从github下载的项目示例有效,但在我创建新项目时却没有.我错过了一步吗? 解决方法
我刚刚检查过这个扩展没有问题(至少没有最新的MVC4预览版):
>创建一个新的MVC4 WEB Api项目 你的代码看起来不错.其他地方一定有问题. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 为什么我的内联asp.net无法正常工作?
- asp.net-mvc – 失败的ASP.NET MVC路由.这是一个错误还是角
- asp.net-mvc – 在ASP.NET MVC应用程序中放置数据操作和业务
- asp.net-core – JsonConvert DataSet .NET Core 2.0无效
- asp.net – web.config使用规则将非www或非https重定向到ht
- asp.net-mvc – 在MVC中检查图像mime,大小等
- asp.net – 防止在ASP .Net中为单个用户进行多次登录
- 当InternalsVisibleToAttribute特性遭遇"强签名"
- asp.net – 可以在.NET Framework和.NET Core之间共享的类库
- 腾讯云-容器-常用操作命令
推荐文章
站长推荐
- ASP.NET MVC下Ajax.BeginForm方式无刷新提交表单
- asp.net – 从信号器js客户端到集线器功能传递连
- asp.net – 使用ASP .Net MVC4处理Web API中的集
- 如何在ASP.NET应用程序中唯一标识客户端计算机?
- 在Asp.Net中使用Office365 SMTP时出错
- 我可以使用MiniProfiler来检测ASP.NET MVC WebAp
- asp.net – 当绑定值包含冒号时,如何绑定GridVie
- asp.net – html元素的命名约定
- asp.net – 我对Html.TextBoxFor的工作原理有什么
- asp.net – 插入多行的最佳方式(ADO.NET)
热点阅读