Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private member is suddenly null on API method call

Weird stuff going on: In my web api, I inject a repository into the controller upon resolving using Ninject. The repository gets stored in a private readonly member variable. Works perfectly fine! When a api method gets called, I access the variable - only to see that it's suddenly null!

Pseudo example:

public class MyController : ApiController {

  private readonly IRepo _repo;

  public MyController(IRepo repo) {
     Guard.AgainstNullArgument("repo", repo); // guarding to 
                                                          // make sure it's not null
                                                          // (would throw ex)
     _repo = repo; <--- successfully injected
  }

  // calling this method
  public HttpResponseMessage TestMethod() {
     _repo.. <--- suddenly null
  }

}

I've traced down the problem to a tiny little detail: One of the methods in the controller (not the one that get's accessed) is annotated with a custom attribute that directs ninject to intercept the method with a unit of work. If I take the attribute away, everything magically works again.

UnitOfWorkAttribute.cs

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Interface)]
public class UnitOfWorkAttribute : Attribute
{
}

AttributeInterceptionStrategy.cs (for ninject)

http://pastebin.com/Qg6tQWye

StartupConfig.cs (composition root, IoC configuration etc.)

http://pastebin.com/fcuSdujj

EfUnitOfWorkInterceptor.cs

public class EfUnitOfWorkInterceptor : SimpleInterceptor
{

    private readonly IUnitOfWork _unitOfWork;

    public EfUnitOfWorkInterceptor(IUnitOfWork unitOfWork)
    {
        Guard.AgainstNullArgument("unitOfWork", unitOfWork);
        _unitOfWork = unitOfWork;
    }

    protected override void AfterInvoke(IInvocation invocation)
    {
        if(!_unitOfWork.Commited)
            _unitOfWork.Commit();

        _unitOfWork.Dispose();
    }
}

EDIT

I've literally put breakpoints everywhere to figure out what's going on. Made a destructor on the controller to make sure the whole class doesn't get garbaged and also changed the readonly member to a property with getter/setter where I break-pointed on the setter to check if it's assigned twice. Nothing suspicuous happens at all.

EDIT 2

Stack

http://pastebin.com/pQULHLT0

Ninject.Extensions.Interception.dll!Ninject.Extensions.Interception.Injection.Dynamic.DynamicMethodInjector.Invoke(object target = {EIT.Management.Configuration.Web.Api.Controllers.SetupGroupController}, object[] arguments = {object[2]})   Unbekannt
    Ninject.Extensions.Interception.dll!Ninject.Extensions.Interception.Invocation.Invocation.CallTargetMethod()    Unbekannt

EDIT 3*

Real world code: http://pastebin.com/SqpR9KNR

like image 941
xvdiff Avatar asked Jan 06 '15 16:01

xvdiff


1 Answers

It's strange so ! I guess maybe you have another constructor which has not set your _repo, then a new instance of the controller instantiated by that.

like image 166
fred Avatar answered Nov 09 '22 02:11

fred