views:

72

answers:

0

I am having problems with the following class in a multi-threaded environment:

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;
    }
}

This is a legacy class which is why I am using property rather than constructor injection.

Sometime when the DoSomething() is called the InjectedBar property is null. In a single-threaded application, everything runs fine.

How can this be occuring and how can I prevent it?

I am using NInject 2.0 without any extensions, although I have copied the KernelContainer from the NInject.Web project.

I have noticed a similar problem occurring in my web services. This problem is extremely intermittent and difficult to replicate.