草庐IT

ninject-3

全部标签

c# - 在 Ninject 中注入(inject)接口(interface)数组

考虑以下代码。publicinterfaceIFoo{}publicclassBar{publicBar(IFoo[]foos){}}publicclassMyModule:NinjectModule{publicoverridevoidLoad(){Bind().ToConstant(newIFoo[0]);//ToConstant()isjustanexample}}publicclassProgram{privatestaticvoidMain(string[]args){varkernel=newStandardKernel(newMyModule());varbar=kern

c# - 在 Ninject 的所有程序集中加载模块

我的项目中有几个类库,并且都在使用NinjectIoC容器。我想在找到INinjectModule的地方一次性加载StandardKernel中的所有模块。所以我用了:varkernel=newStandardKernel();kernel.Load(AppDomain.CurrentDomain.GetAssemblies())但是由于某些原因这不起作用。谁能帮忙? 最佳答案 好吧,这通常发生在声明了绑定(bind)但加载了其他模块时,该模块试图解析尚未加载的绑定(bind)。发生这种情况是因为List顺序可能不正确。如果你认为是

c# - 设置 Ninject 单例的方法

我有一个类(MyFacade),我用Ninject注入(inject)了参数:classMyFacade{IDemoInterfacedemo;publicMyFacade(IDemoInterfacedemo){this.demo=demo;}publicvoidMyMethod(){Console.WriteLine(demo.GetInfo());}}当然,我必须设置Ninject注入(inject)我的参数的适当实现(IDemoInterface)我知道,我可以实例化MyFacade通过做对象kernel.Get();无需设置任何其他内容。目前我的门面没有接口(interfac

c# - 如何解决 Ninject 模块内的依赖关系?

我正在使用Ninject2和Asp.NetMVC3。我有以下模块。publicclassServiceModule:NinjectModule{publicoverridevoidLoad(){//Ineedtogetthe'configHelper'fromNinjectContainer.IConfigHelperconfigHelper=ResolveConfigHelperHere();Bind().To().WithConstructorArgument("myArg",configHelper.MyProperty);}}我们如何获取已在Ninject模块中绑定(bind)

c# - 如何在 ASP.NET MVC 5 中将 IAuthenticationManager 与 Ninject 绑定(bind)?

我正在尝试将IAuthenticationManager与Ninject绑定(bind),以便它可以注入(inject)到我的AuthenticationService中。问题是我目前从HttpContext.GetOwinContext()在我的Controller上获取IAuthenticationManager,如下所示:privateIAuthenticationManagerAuthenticationManager{get{returnthis.HttpContext.GetOwinContext().Authentication;}}我如何着手创建与Ninject的绑定(

c# - MVC3 + Ninject - 如何?

我刚刚开始使用IoC容器,因此选择了Ninject。经过几个小时的汗水和泪水,我仍然无法弄清楚如何使用Ninject设置我的MVC3应用程序。到目前为止我有:全局.asax.cspublicclassMvcApplication:Ninject.Web.Mvc.NinjectHttpApplication{publicstaticvoidRegisterGlobalFilters(GlobalFilterCollectionfilters){filters.Add(newHandleErrorAttribute());}publicstaticvoidRegisterRoutes(Ro

c# - 初始化 ninject 时“序列不包含任何元素”异常

我遇到了Ninject的问题,这是我以前从未见过的,也不知道如何解决。我正在为MVC5使用MVCNuGet包。在我的NinjectWebCommon.cs中,有如下方法:publicstaticvoidStart(){DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));bootstrapper.Initialize(CreateKernel);}然而,它在尝试执行最后一行时失败

c# - 如何在 Windows 窗体应用程序中使用 Ninject?

我有一个带有此主窗体的WinForms应用程序:ICountRepositorycountRepository;publicMainForm(ICountRepositorycountRepository){this.countRepository=countRepository;}publicvoidIncrementCount(){countRepository.IncrementCount();}但我正在努力将ICountRepository注入(inject)主窗体。我该怎么做? 最佳答案 好吧,第一步是从:varform=

c# - Ninject.MVC5 不生成 NinjectWebCommon.Cs

我正在VisualStudio2017版本15.4上开发MVC5项目。我在这里得到了意想不到的结果,这是我以前从未遇到过的。我已经从nuget安装了Ninject.MVC5包。它安装得很好,没有给出任何错误或警告。但问题是它没有在App_Start文件夹中生成NinjectWebCommon.cs文件。有什么理由吗? 最佳答案 看起来最新的Ninject.Web.Common.WebHost3.3.0NuGet包不再包含NinjectWebCommon.cs。旧版本(例如3.2.0)确实包含此文件。Ninject.Web.Commo

Ninject工厂扩展与异步等待

如果我使用这样的工厂接口publicinterfaceIBoredFactory{IBoredCreate()}在装订中这样做kernel.Bind().ToFactory();kernel.Bind().To();它正如预期的那样工作,代码返回打哈欠但是,如果我将工厂界面更改为此publicinterfaceIBoredFactory{TaskCreate()}工厂代理将建造,但线程永远不会从Create(),呼叫者无休止地等待着。我在这里错过了什么吗?看答案正如@andreasAppelros所建议的Ninject正在返回未完成的Task.由于实际上没有异步ninjectapi,即使在创建