Property setter injection [Inject] Not working

497 Views Asked by At

I am trying to use property Setter injection method, in the FilterAttribute class

Below is my AttributeFilter

 public class HasIfNoneMatchAttribute : ActionFilterAttribute
 {
    [Inject]
    public ICorporationRepository _corporationRepository { set; private get;    }

    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        var request = actionContext.Request;

        ICollection<EntityTagHeaderValue> etagsFromClient = request.Headers.IfNoneMatch;

        if (etagsFromClient.Count > 0)
        {
            EntityTagHeaderValue etag = new EntityTagHeaderValue(this._corporationRepository.GetETagForCorporationRequirement("param1", "param2", "param3"));
            if (etagsFromClient.Contains(etag))
            {
                actionContext.Response = new HttpResponseMessage(HttpStatusCode.NotModified);
               // SetCacheControl(context.Response);
            }
        }

        base.OnActionExecuting(actionContext);
    }}
}

I am already using constructor injection in the project. which works fine. Below is part of my NinjectWebCommon.cs file

public static class NinjectWebCommon 
{
    private static readonly Bootstrapper bootstrapper = new Bootstrapper();

    public static void Start() 
    {
          DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
        DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
        bootstrapper.Initialize(CreateKernel);
    }

    public static void Stop()
    {
        bootstrapper.ShutDown();
    }

    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

        RegisterServices(kernel);
        return kernel;
    }

    private static void RegisterServices(IKernel kernel)
    {

        kernel.Bind<ICorporationRepository>().To<CorporationRepository>();
}

Property is not getting injected, and I get the error - System.NullReferenceException: Object reference not set to an instance of an object.

What am I missing?

0

There are 0 best solutions below