我最近尝试了Ninject与Ninject.Web.Mvc扩展,我已经发现了一些奇特的和,而不是突破,令人目不暇接.
在NinjectHttpApplication抽象类中,有一个构造函数定义如下..
/// <summary>
/// Initializes a new instance of the <see cref="NinjectHttpApplication"/> class.
/// </summary>
protected NinjectHttpApplication()
{
this.onePerRequestModule = new OnePerRequestModule();
this.onePerRequestModule.Init(this);
}
Run Code Online (Sandbox Code Playgroud)
我在这里放置了一个调试器断点,这会被调用几次.我找不到任何真实的文档.在实现代码中,有这条线引起了我的注意.
if (kernel.Settings.Get("ReleaseScopeAtRequestEnd", true))
{
OnePerRequestModule.StartManaging(kernel);
}
Run Code Online (Sandbox Code Playgroud)
我的问题如下......
OnePerRequestModuleStartManaging如果构造函数被多次调用,那么此方法的目的是什么?有没有人有关于如何在WCF中设置Ninject的明确指示?谷歌搜索,但我看不到有关如何在WCF中使用Ninject的任何更新指南.
最近,我将我的一个MVC3项目从Ninject 2升级到了Ninject 3.
在几分钟后试图找到为什么InRequestScope不再可用,我发现这现在是Ninject.Web.Common的扩展.
现在,当我尝试运行应用程序时,Ninject的工作方式就像所有与范围InRequest绑定的类型都是InTransientScope; 每次都创建一个新实例.
在我继承自NinjectModule的类中,我有一个简单的绑定:
Bind<ViewModel.Activity>().ToSelf().InRequestScope();
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我有2个标有Ninject属性的ViewModel.Activity类型的属性.
[Inject]
public ViewModel.Activity Activity { get; set; }
[Inject]
public ViewModel.Activity Activity1 { get; set; }
Run Code Online (Sandbox Code Playgroud)
如果我在调试模式中查看两个属性的HashCode的值,那么它们都有不同的值但是HttpContext是相同的; 我在同一个请求中.
我错过了如何使用Ninject 3的新版本正确使用新的Ninject.Web.Common.InRequestScope?
非常感谢你.
我正在使用MVC3和Ninject启动Web应用程序.在Global.asax文件中还需要一个依赖项,它需要是一个单例.
我认为应该是这样的:
public class MvcApplication : NinjectHttpApplication
{
IUserAuthentication _auth;
public MvcApplication()
{
base.AuthenticateRequest += new EventHandler(MvcApplication_AuthenticateRequest);
}
protected override IKernel CreateKernel()
{
var _kernel = new StandardKernel(new SecurityModule());
_auth = _kernel.Get<IUserAuthentication>();
return _kernel;
}
void MvcApplication_AuthenticateRequest(object sender, EventArgs e)
{
_auth.ToString();
}
Run Code Online (Sandbox Code Playgroud)
但后来我看到它被调用_auth时MvcApplication_AuthenticateRequest为null .
然后我试着这样:
public class MvcApplication : NinjectHttpApplication
{
ItUserAuthentication _auth;
IKernel _kernel;
public MvcApplication()
{
_kernel = new StandardKernel(new SecurityModule());
_auth = _kernel.Get<IUserAuthentication>();
base.AuthenticateRequest += new EventHandler(MvcApplication_AuthenticateRequest);
}
protected override IKernel CreateKernel() …Run Code Online (Sandbox Code Playgroud) 我的解决方案中有两个项目......域项目和MVC3 Web项目(例如MyApp.Domain和MyApp.Web).以前,当使用Ninject.Extensions.Conventions ver.2,我能够在NinjectMVC3.cs文件中使用以下语句,并且在我的解决方案(web和域)中所需的依赖项被正确注入(例如,IFoo自动绑定到Foo).
kernel.Scan(x =>
{
x.FromAssembliesMatching("*");
x.BindWith<DefaultBindingGenerator>();
});
Run Code Online (Sandbox Code Playgroud)
我刚刚升级到Ninject 3.0.0(预发布)和Ninject.Extensions.Conventions 3.0.0(另一个预发行版),但基于约定的绑定的语法已经改变.我已经发现我可以在新版本中使用以下语句,但它只会自动绑定MyApp.Web中基于约定的接口而不是MyApp.Domain.以前的版本绑定了整个应用程序的接口.
kernel.Bind(x => x
.FromThisAssembly()
.SelectAllClasses()
.BindToAllInterfaces());
Run Code Online (Sandbox Code Playgroud)
我是如何使用新的Ninject版本配置基于约定的绑定的?我假设它与指定程序集有关,但我已经尝试过使用,FromAssembliesMatching("*")然后它就失败了.
- 编辑以在RegisterServices方法中显示我现有的代码: -
private static void RegisterServices(IKernel kernel)
{
// This code used to work with v.2 of Ninject.Extensions.Conventions
// kernel.Scan(x =>
// {
// x.FromAssembliesMatching("*");
// x.BindWith<DefaultBindingGenerator>();
// });
// This is the new v3 code that automatically injects dependencies but only for interfaces in MyApp.Web, not MyApp.Domain
kernel.Bind(x => x.FromThisAssembly().SelectAllClasses().BindToAllInterfaces());
// I tried this code, but …Run Code Online (Sandbox Code Playgroud) 我目前正在使用Ninject(2.2.1.4)和Ninject.Extensions.Wcf(2.2.0.4)和我的WCF服务.我想升级到Ninject(3.0.0.15)和Ninject.Extensions.Wcf(3.0.0.5),看起来我不能再使用我当前的方法了.任何人都可以向我指出一些样本或帖子,了解如何使用WCF项目获得最新版本的Ninject.
我目前的做法:
我写了一个模块:
public class NinjectDependencyResolver : NinjectModule
{
public override void Load()
{
// Declare bindings
}
}
Run Code Online (Sandbox Code Playgroud)
我将Factory属性添加到我的.svc文件中
Factory="Ninject.Extensions.Wcf.NinjectServiceHostFactory"
Run Code Online (Sandbox Code Playgroud)
我在WCF项目中添加了一个Global.asax
public class Global : NinjectWcfApplication
{
protected override IKernel CreateKernel()
{
return new StandardKernel(new NinjectDependencyResolver());
}
}
Run Code Online (Sandbox Code Playgroud)
现在我可以修改我的服务中的默认构造函数并使用构造函数注入.
关于如何升级的任何指示都表示赞赏.
谢谢
我收到以下错误:
Test method: BootStrapperTest.Can_Create_Alert_Management_Object threw exception: Ninject.ActivationException:
Error activating IAlertManagement No matching bindings are available, and the type is not self-bindable.
Activation path:
1) Request for IAlertManagement
Suggestions:
1) Ensure that you have defined a binding for IAlertManagement.
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 …Run Code Online (Sandbox Code Playgroud) 我已经注意到在截取教程中你可以定位一个方法并拦截它.即
Kernel.Bind<Foo>().ToSelf();
Kernel.InterceptReplace<Foo>(foo => foo.ThrowsAnError(), invocation => {} );
Run Code Online (Sandbox Code Playgroud)
文档/教程没有介绍在您尝试拦截的方法具有参数的实例中要执行的操作,即ThrowsAnError是否接受字符串作为参数.
Kernel.Bind<Foo>().ToSelf();
Kernel.InterceptReplace<Foo>(foo => foo.ThrowsAnError(**param goes here**), invocation => {} );
Run Code Online (Sandbox Code Playgroud)
在绑定时我无法访问params,所以我想知道我是否会以错误的方式进行此操作?
编辑
c# dependency-injection ninject ninject-extensions ninject-interception
我主要使用Ninject作为手动绑定,如波纹管.哪个工作正常
kernel.Bind<TestContext>().ToSelf().InRequestScope();
kernel.Bind<ITestRepository>().To<TestRepository>();
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用基于约束的绑定绑定时,有点困惑,什么时候使用?
我浏览了这个Ninject文档但是我找不到很多例子.
根据我的所有Repository类实现IRepository < Model >.因此,如果我想以传统方式进行绑定,那么下面的代码可以正常工作.
kernel.Bind(x => x
.FromAssembliesMatching("*")
.SelectAllClasses()
.InheritedFrom(typeof(IRepository<>))
.BindDefaultInterface());
Run Code Online (Sandbox Code Playgroud)
但我有点困惑
1.当我改变.FromAssembliesMatching("*") 要.FromThisAssembly()它不能正常工作,并抛出Error activating ITestRepository为什么呢?
2.当改变.SelectAllClasses()为To .SelectAllIncludingAbstractClasses()时,.FromAssembliesMatching("*")它的组合工作正常,为什么?
让我解释一下我的代码结构.
IRepository(在DLL 1中)
public interface IRepository<E>
{
E Get();
}
Run Code Online (Sandbox Code Playgroud)
RepositoryBase(在DLL 1中)
public abstract class RepositoryBase<E> : IRepository<E>
where E : class
{
public E Get()
{
return System.Activator.CreateInstance<E>(); // this is just for testing
}
}
Run Code Online (Sandbox Code Playgroud)
TestRepository …
c# dependency-injection ninject ninject-extensions ninject.web.mvc
(这个问题最初是在Ninject Google Group中提出的,但我现在看到Stackoverflow似乎更活跃了.)
我正在使用NamedScopeExtension将相同的ViewModel注入到View和Presenter中.发布View后,内存分析显示Ninject缓存仍保留ViewModel.如何让Ninject发布ViewModel?表单关闭和处置时释放所有ViewModel,但我使用表单中的Factory创建和删除控件,并希望将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();
// Assert
dotMemory.Check(m =>
{
m.GetObjects(w => w.Type.Is<ViewModel>()).ObjectsCount.Should().Be(0);
});
}
private static void SubMethod()
{
// Arrange
var kernel = new StandardKernel();
string namedScope = "namedScope";
kernel.Bind<View>().ToSelf()
.DefinesNamedScope(namedScope);
kernel.DefineDependency<View, …Run Code Online (Sandbox Code Playgroud)