Tag: ninject extensions

Ninject在具有多个程序集的WebApi项目中抛出Activation Exception

我的asp.net WebApi项目包含多个服务,核心和数据访问程序集。 为了在项目中使用Ninject作为我的DI容器,我从NuGet添加了Ninject.Web.Common包。 然后,我实现了IDependencyResolver: public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver { readonly IKernel kernel; public NinjectDependencyResolver(IKernel kernel) : base(kernel) { this.kernel = kernel; } public IDependencyScope BeginScope() { return new NinjectDependencyScope(this.kernel.BeginBlock()); } } public class NinjectDependencyScope : IDependencyScope { IResolutionRoot resolver; public NinjectDependencyScope(IResolutionRoot resolver) { this.resolver = resolver; } public object GetService(System.Type serviceType) { if […]

NamedScope和垃圾收集

(这个问题首先在Ninject Google Group中提出,但我现在看到Stackoverflow似乎更活跃了。) 我正在使用NamedScopeExtension将相同的ViewModel注入View和Presenter。 释放View后,内存分析显示Ninject缓存仍保留ViewModel。 如何让Ninject释放ViewModel? 当Form关闭和处置时,所有ViewModel都会被释放,但我正在使用Form中的Factory创建和删除Controls,并希望将ViewModel垃圾收集到(收集Presenter和View)。 有关问题的说明,请参阅以下UnitTest,使用dotMemoryUnit: using System; using FluentAssertions; using JetBrains.dotMemoryUnit; using Microsoft.VisualStudio.TestTools.UnitTesting; using Ninject; using Ninject.Extensions.DependencyCreation; using Ninject.Extensions.NamedScope; namespace UnitTestProject { [TestClass] [DotMemoryUnit(FailIfRunWithoutSupport = false)] public class UnitTest1 { [TestMethod] public void TestMethod() { // Call in sub method so no local variables are left for the memory profiling SubMethod(); // […]

如何使用Ninject Conventions Extension进行绑定?

我喜欢使用Ninject自动绑定绑定波纹管代码。 是否可以在单个项目中同时使用手动和自动绑定? 让我们采取波纹管手动绑定,我希望通过自动绑定实现。 请告诉我如何实现这一目标。 kernel.Bind().ToSelf().InRequestScope(); kernel.Bind<IUnitOfWork>().To<UnitOfWork>(); Bellow所有接口都inheritance自基础接口: IRepository 3。 kernel.Bind().To(); 4。 kernel.Bind().To(); 5。 kernel.Bind().To().WithConstructorArgument(“apikey”, AppSettingsManager.GetSmsApiKey) 额外 我是否需要为多个类编写.Exclude()如果我需要这样做的话 .Exclude() .Exclude() .Exclude() ? 并且对于1和2是否需要单独的手动绑定? 或者1可以使用BindToSelf()’ and .Configure(b => b.InRequestScope())`完成?

将行为属性添加到WorkflowServiceHost

大家好我在向WorkflowServiceHost添加自定义行为时遇到问题。 这是我的WorflowServiceHostFactory: public class ScoringWorkflowServiceHostFactory : WorkflowServiceHostFactory, IServiceHost { private static IKernel _InjectionInstance; public IKernel InjectionInstance { get { return _InjectionInstance ?? (_InjectionInstance = new StandardKernel(new ScoringWorkflowServicesNinjectModule(Scope))); } } public object Scope { get { return Guid.NewGuid(); } } public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses) { String fullFilePath = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, constructorString); WorkflowService wf = […]

获取ninject工厂扩展以允许将工厂参数传递给依赖项

使用Ninject Factory扩展,您可以自动生成工厂,并让工厂将参数传递给类的构造函数。 以下测试通过: public interface IBar { int Foo { get; } } public class Bar : IBar { int _foo; public Bar(int foo) { _foo = foo; } public int Foo { get { return _foo; } } } public interface IBarFactory { IBar CreateTest(int foo); } [Test] public void ExecuteTest() { var kernel […]

为WCF设置Ninject

有没有人有关于如何在WCF中设置Ninject的明确指示? 谷歌搜索,但我看不到有关如何在WCF中使用Ninject的任何更新指南。

Ninject Conventions是否仅适用于公共课程?

我开始在我的项目中使用Ninject来自动绑定抽象类的所有子类。 对此的绑定是 – 简单易行 – 如下: kernel.Bind(x => x.FromThisAssembly() .SelectAllClasses(). .InheritedFrom() .BindBase()); 但是,我发现这不起作用。 经过一些实验,我发现这个不起作用的原因是我的所有实现(和抽象类)都标记为内部 。 我可以想象这是一些安全function,以防止绑定从内部泄漏到外部。 但我可以为这些类添加显式绑定。 因此,我的问题是:有人知道这是否是预期的行为? 有没有办法解决这个问题,除了让我的所有课程都公开?

Ninject 3 InRequestScope不为同一请求返回相同的实例

最近,我将我的一个MVC3项目从Ninject 2升级到Ninject 3。 在几分钟后试图找到为什么InRequestScope不再可用,我发现这现在是Ninject.Web.Common的扩展。 现在,当我尝试运行应用程序时,Ninject的工作方式就像所有与范围InRequest绑定的类型都是InTransientScope; 每次都会创建一个新实例。 在我inheritance自NinjectModule的类中,我有一个简单的绑定: Bind().ToSelf().InRequestScope(); 在我的控制器中,我有2个标有Ninject属性的ViewModel.Activity类型的属性。 [Inject] public ViewModel.Activity Activity { get; set; } [Inject] public ViewModel.Activity Activity1 { get; set; } 如果我在调试模式中查看两个属性的HashCode的值,那么它们都有不同的值,但HttpContext是相同的; 我在同一个请求中。 我错过了如何使用Ninject 3的新版本正确使用新的Ninject.Web.Common.InRequestScope? 非常感谢你。