public class Foo
{
[Inject]
public IBar InjectedBar { get; set; }
public bool NonInjectedProp { get; set; }
public void DoSomething()
{
/* The following line is causing a null-reference exception */
InjectedBar.DoSomething();
}
public Foo(bool nonInjectedProp)
{
/* This line should inject the InjectedBar property */
KernelContainer.Inject(this);
NonInjectedProp = nonInjectedProp;
}
}
這是一個遺留類,這是爲什麼我使用的財產,而不是構造函數注入。
有時,當調用DoSomething()時,InjectedBar屬性爲null。在單線程應用程序中,一切運行良好。
這怎麼可能發生,我該如何防止它呢?
雖然我已經從NInject.Web項目複製了KernelContainer,但我使用NInject 2.0時沒有任何擴展名。
我注意到我的web服務中發生了類似的問題。這個問題非常時斷時續,難以複製。
你的環境是什麼?即你在WCF中託管?什麼版本的ninject?你在用什麼擴展項目? – 2010-06-02 13:39:34
你可以在單元測試中複製嗎? – 2010-06-02 14:01:46
NInject 2.0,不支持擴展。 KernelContainer與NInject.Web中的相同。 – cbp 2010-06-03 00:49:59