I'm developing an ASP.NET Web Api 2.2 web service with .NET Framework 4.6.2.
Yes, there are a lot of questions similar to mine but and I did it everything they suggest in their answers and I have the following error:
{
"Message": "Error.",
"ExceptionMessage": "Error trying to create a controller of type 'PrintedCodesStatisticsController'. Ensure that the controller has a public constructor without parameters.",
"ExceptionType": "System.InvalidOperationException",
"StackTrace": " en System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n en System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n en System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()",
"InnerException": {
"Message": "Error.",
"ExceptionMessage": "Type 'MyProject.WebApi.Controllers.PrintedCodesStatisticsController' doesn't have a predefined constructor",
"ExceptionType": "System.ArgumentException",
"StackTrace": " en System.Linq.Expressions.Expression.New(Type type)\r\n en System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)\r\n en System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n en System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"
}
}
The packages that I'm using on my project are:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.1.3" targetFramework="net462" />
<package id="log4net" version="2.0.5" targetFramework="net462" />
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net462" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net462" />
<package id="Microsoft.AspNet.WebApi.Client.es" version="5.2.3" targetFramework="net462" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net462" />
<package id="Microsoft.AspNet.WebApi.Core.es" version="5.2.3" targetFramework="net462" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net462" />
<package id="Microsoft.AspNet.WebApi.WebHost.es" version="5.2.3" targetFramework="net462" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net462" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net462" />
<package id="Ninject" version="3.2.2.0" targetFramework="net462" />
<package id="Ninject.MVC5" version="3.2.1.0" targetFramework="net462" />
<package id="Ninject.Web.Common" version="3.2.3.0" targetFramework="net462" />
<package id="Ninject.Web.Common.WebHost" version="3.2.3.0" targetFramework="net462" />
<package id="WebActivatorEx" version="2.2.0" targetFramework="net462" />
</packages>
And I'm sure I'm using NinjectDependenciaResolver because I have debugged it and it stops on it sets GlobalConfiguration.Configuration.DependencyResolver.
NinjectDependencyResolver is:
using Ninject;
using System.Web.Http.Dependencies;
namespace AnotherProject.Web.Common.Ninject
{
/// <summary>
/// Used to resolve dependencies.
/// </summary>
public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
{
public NinjectDependencyResolver(IKernel kernel)
: base(kernel)
{ }
public IDependencyScope BeginScope()
{
return this;
}
}
}
And NinjectDependencyScope is:
public class NinjectDependencyScope : IDependencyScope
{
private readonly IKernel _container;
public IKernel Container
{
get { return _container; }
}
public NinjectDependencyScope(IKernel container)
{
_container = container;
}
public object GetService(Type serviceType)
{
return _container.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return _container.GetAll(serviceType);
}
public void Dispose()
{
// noop
}
}
And finally, NinjectConfigurator is:
public class NinjectConfigurator
{
/// <summary>
/// Entry method used by caller to configure the given
/// container with all of this application's
/// dependencies. Also configures the container as this
/// application's dependency resolver.
/// </summary>
public void Configure(IKernel container)
{
// Add all bindings/dependencies
AddBindings(container);
// Use the container and our NinjectDependencyResolver as
// application's resolver
var resolver = new NinjectDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = resolver;
}
/// <summary>
/// Add all bindings/dependencies to the container
/// </summary>
private void AddBindings(IKernel container)
{
[ ... ]
}
}
How can I solve this error?
NinjectConfiguratorclass.