I am receiving an exception from Ninject following a code refactor. therefore I know what is causing the error but cannot determine why. The code compiles file but I get a runtime error from Ninject:
Cannot be null Parameter name: root
I am trying to inject a constant instance of a class:
var subscriptionApiClientHelper = new SubscriptionApiClientHelper(AppSettings.Get<string>("AdminAPIUrl"),
AppSettings.Get<string>("APIV2Key"), Kernel.Get<ICache>());
kernel.Bind<SubscriptionApiClientHelper>().ToConstant(subscriptionApiClientHelper);
Ninject throws the exception on the first line.
My class SubscriptionApiClientHelper inherits from another class (referenced in a Nuget Package) thus:
public SubscriptionApiClientHelper(string baseUrl, string apiKey, ICache cache) : base(baseUrl, apiKey)
{
_cache = cache;
}
The constructor of which is this:
public SubscriptionApiClient(string baseUrl, string apiKey)
{
_baseUrl = baseUrl;
_apiKey = apiKey;
}
A couple of methods in this base class are overridden and caching implemented, hence the reason for overriding as I do not need to implement caching on all the methods of the base class. I am injecting some other classes from the same Nuget package without issue though, these do not have the inheritance that I suspect is causing the issue. Note I am successfully injecting and using the base class I am inheriting also (SubscriptionApiClient), it is just the helper class that is causing the issue.
This is the full stack trace:
at Ninject.ResolutionExtensions.GetResolutionIterator(IResolutionRoot root, Type service, Func`2 constraint, IEnumerable`1 parameters, Boolean isOptional, Boolean isUnique)
at Ninject.ResolutionExtensions.Get[T](IResolutionRoot root, IParameter[] parameters)
at AdminPortal.NinjectWebCommon.RegisterServices(IKernel kernel) in C:\Projects\AdminPortal\AdminPortal\App_Start\NinjectWebCommon.cs:line 106
at AdminPortal.NinjectWebCommon.CreateKernel() in C:\Projects\AdminPortal\AdminPortal\App_Start\NinjectWebCommon.cs:line 72
at Ninject.Web.Common.Bootstrapper.Initialize(Func`1 createKernelCallback)
at AdminPortal.NinjectWebCommon.Start() in C:\Projects\AdminPortal\AdminPortal\App_Start\NinjectWebCommon.cs:line 39
Does anyone know what is going wrong?
Your Kernel variable is assigned to null reference. Because Get is an extension method, you won't get NullReferenceException, but ArgumentNullException instead. I'm getting the same exception when running this test:
StandardKernel kernel = null;
kernel.Get<ICache>();
this was down to my misuse of Ninject when trying to construct the instance of the class I wanted to inject. I solved it thus:
kernel.Bind<SubscriptionApiClientHelper>().To<SubscriptionApiClientHelper>()
.WithConstructorArgument("baseUrl", AppSettings.Get<string>("AdminAPIUrl"))
.WithConstructorArgument("apiKey", AppSettings.Get<string>("APIV2Key"))
.WithConstructorArgument("cache", kernel.Get<ICache>());
I can now inject this class successfully.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With