In my ASP .NET MVC WebApi project I have controller constructor that receive a parameter, I don't have a prameterless constructor.
The dependecies are injected using Ninject:
- Ninject 3.2.0.0 (3.2.2)
- Ninject.Web.Common 3.2.0.0 (3.2.3)
- Ninject.Web.WebApi 3.2.0.0 (3.2.4)
When I call the API I get:
An exception of type 'Ninject.ActivationException' occurred in Ninject.dll but was not handled in user code
Additional information: Error activating IHubRepository
No matching bindings are available, and the type is not self-bindable.
Activation path:
2) Injection of dependency IHubRepository into parameter repHub of constructor of type ResultController
1) Request for ResultController
Suggestions:
1) Ensure that you have defined a binding for IHubRepository.
2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
3) Ensure you have not accidentally created more than one kernel.
4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.
5) If you are using automatic module loading, ensure the search path and filters are correct.
The code is the following.
Controller:
public class ResultController : BaseHubController
{
public ResultController(IHubRepository repHub)
{
_rep = repHub;
}
public async Task<IHttpActionResult> Get(string fileName)
{
if (String.IsNullOrEmpty(fileName))
return BadRequest();
return Ok();
}
}
NinjectResolver
public sealed class NinjectResolver : NinjectScope, IDependencyResolver
{
private IKernel kernel;
public NinjectResolver(IKernel kernelParam)
: base(kernelParam)
{
kernel = kernelParam;
AddBindings();
}
public IDependencyScope BeginScope()
{
return new NinjectScope(kernel.BeginBlock());
}
public void AddBindings()
{
kernel.Bind<Hub.Dal.IHubRepository>().To<Hub.Dal.HubRepository>();
kernel.Bind<System.Data.Entity.DbContext>().To<Hub.Dal.Context>();
}
}
public class NinjectScope : IDependencyScope
{
protected IResolutionRoot resolutionRoot;
public NinjectScope(IResolutionRoot kernel)
{
resolutionRoot = kernel;
}
public object GetService(Type serviceType)
{
IRequest request = resolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
return resolutionRoot.Resolve(request).SingleOrDefault();
}
public IEnumerable<object> GetServices(Type serviceType)
{
IRequest request = resolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
return resolutionRoot.Resolve(request).ToList();
}
public void Dispose()
{
IDisposable disposable = (IDisposable)resolutionRoot;
if (disposable != null) disposable.Dispose();
resolutionRoot = null;
}
}
IHubRepository
public interface IHubRepository
{
bool LogRequest(SHApiLog logData);
}
HubRepository
public class HubRepository : IHubRepository
{
private DbContext _ctx;
public HubRepository(DbContext curCtx)
{
this._ctx = curCtx;
}
public bool LogRequest(SHApiLog logData)
{
try
{
_ctx.Set<SHApiLog>().Add(logData);
_ctx.SaveChanges();
}
catch (Exception)
{
return false;
}
return true;
}
}
I can't understand why it won't work.
ISistemiHubRepository
as you can see here:Injection of dependency ISistemiHubRepository into parameter repHub of constructor of type ResultController
. It is not complaining aboutIHubRepository
– CodingYoshipublic HubRepository(DbContext curCtx)
because it needs a parameter to be instantiated. Do a test and put an empty constructor there and see if the error goes away, if it does then you know that is the issue. Then to fix the issue, you may need to use this during binding:.WithConstructorArgument("curCtx", .new DbContext /*or whatever*/);
– CodingYoshi