I have a WebAPI project in .Net Framework 4.8. I want to add Ninject to this project and I am facing various issues. There are many answers on Stackoverflow but none of them could solve my error.
First of all, I installed the following packages in my API Project.
Ninject (3.3.6)
Ninject.Web.Common (3.3.2)
Ninject.Web.Common.WebHost (3.3.2)
Ninject.Web.WebApi (3.3.1)
Ninject.Web.WebApi.WebHost (3.3.1)
And after installing them, NinjectWebCommon class came under App_Start folder.
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();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<VersionOperations>().ToSelf();
}
}
After this adjustment, I made the necessary matching in the constructor of my ApiController.
public class AdminController : ApiController
{
private readonly VersionOperations _versionOperations;
public AdminController(
VersionOperations versionOperations)
{
_versionOperations = versionOperations;
}
[HttpGet]
[Route("Test")]
public ApiResultBase Test()
{
return new ApiResultBase();
}
When I do these, I finally get the following error from Postman.
"Message": "An error has occurred.",
"ExceptionMessage": "An error occurred when trying to create a controller of type 'AdminController'. Make sure that the controller has a parameterless public constructor.",
"ExceptionType": "System.InvalidOperationException",
Here is the solution https://continuousdeveloper.com/2016/06/30/using-ninject-with-webapi-2-0/
I tried this friend's solution. But here too I get the following error while the project is standing up
Error activating ModelValidatorProvider using binding from ModelValidatorProvider to NinjectDefaultModelValidatorProvider A cyclical dependency was detected between the constructors of two services.
Related
I am trying to learn MVC and I started out with the book Pro ASP.NET MVC by Adam Freeman (5th edition #2013).
In chapter VII I'm trying to follow the example in the book making a small app.
The app fails to load after setting it up and trying to load a list of products.
I'm trying to create a mock implementation of the abstract repository IProductRepository and have Ninject return the mock object whenever it gets a request for an implementation of the IProductRepository interface.
I've searched and looked over other questions/ answers and found nothing that could help solve my problem and let me move forward with studying. This may be basic stuff but I really want to know what and why is not working as it should.
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
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;
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}
}
}
This next is my NinjectDependencyResolver class:
public class NinjectDependencyResolver : IDependencyResolver
{
private IKernel kernel;
[Inject]
public NinjectDependencyResolver(IKernel kernelParam)
{
kernel = kernelParam;
AddBindings();
}
public object GetService(Type serviceType)
{
return kernel.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return kernel.GetAll(serviceType);
}
private void AddBindings()
{
var mock = new Mock<IProductsRepository>();
mock.Setup(m => m.Products).Returns(new List<Product>
{
new Product { Name = "Fotball", Price = 25 },
new Product { Name = "Surf Board", Price = 45 },
new Product { Name = "Running Shoes", Price = 95 }
});
kernel.Bind<IProductsRepository>().ToConstant(mock.Object);
}
}
And this is my controller class:
public class ProductController : Controller
{
private IProductsRepository repository;
public ProductController(IProductsRepository productRepository)
{
repository = productRepository;
}
public ViewResult List()
{
return View(repository.Products);
}
The error I get is the following:
Error activating IProductsRepository
No matching bindings are available, and the type is not self-bindable.
Activation path:
2) Injection of dependency IProductsRepository into parameter productRepository of constructor of type ProductController.
1) Request for ProductController.
Suggestions:
1) Ensure that you have defined a binding for IProductsRepository.
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.
OK, so it seems that I had another error which said:
Found conflicts between different versions of the same dependent assembly that could not be resolved
I had installed the specific versions of Ninject, Ninject.Web.Common, Ninject.MVC3, Moq and the other packages as specified by the book author.
After reading the error in the build output I tried updating all installed packages to the latest versions, rebuild the project and it all worked just fine!
I have a web project containing 3 layers: Web (MVC5), BusinessLayer, DataAccess. I use StructureMap 4, Structuremap.MVC5 and StructureMap.WebApi2 to provide the default IoC configuration.
This is my configuration:
public static class IoC {
public static IContainer Initialize() {
var container = new Container(c => c.AddRegistry<DefaultRegistry>());
return container;
}
}
public class DefaultRegistry : Registry {
public DefaultRegistry() {
this.IncludeRegistry<DataAccessLayerRegistry>();
this.IncludeRegistry<BusinessLayerRegistry>();
Scan(
scan => {
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.With(new ControllerConvention());
});
}
}
The DataAccessLayerRegistry and BusinessLayerRegistry don't really do anything apart from scanning their respective dlls with DefaultConventions
Everything else is as generated by templates.
I inject dependencies in such hierarchical way:
Web:
public class HomeController : Controller
{
private ITestClass _myTest;
public HomeController(ITestClass testClass)
{
_myTest = testClass;
}
}
BusinessLayer:
public class TestClass : ITestClass
{
public TestClass(ITestValueRepository repo)
{
}
}
DataAccess:
public class TestValueRepository : ITestValueRepository
{
IMyContext _dbContext;
public TestValueRepository(IMyContext dbContext)
{
_dbContext = dbContext;
}
}
This all works fine and the dependencies are resolved correctly but when there is an error in one of the constructors somewhere down the road, for example an error creating the IMyContext instance (which is an EntityFramework DbContext), I don't get to see the real exception that happened there (for example issue with EF configuration). Instead this is what I see:
No parameterless constructor defined for this object.
[InvalidOperationException: An error occurred when trying to create a
controller of type 'XXX.Web.Controllers.HomeController'. Make sure
that the controller has a parameterless public constructor.]
There is no inner exception nor additional stack trace info that could lead to the real problem. Why is StructureMap hiding the real exception? Is there any way that I can set the StructureMap configuration to make it throw the real exceptions?
I'm using NinjectWebCommon to perform the injections in my controllers. I installed the package via Nuget and he created the NinjectWebCommon.cs in my App_Start as it says in the own documentation. I need to know why it does not work as it should, because I follow the documentation step by step. Follows some snippets:
NinjectWebCommon.cs:
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
//kernel.Load(AppDomain.CurrentDomain.GetAssemblies());
kernel.Bind<IFooService>().To<FooService>();
}
}
Controller:
public class FooController : Controller
{
private readonly IFooService fooService;
public FooController(IFooService fooService)
{
this.fooService = fooService;
}
public ActionResult Index()
{
return View(this.fooService.All());
}
}
This generates this error:
Error activating IFooService No matching bindings are available, and
the type is not self-bindable. Activation path:
2) Injection of
dependency IFooService into parameter fooService of constructor of
type FooController
1) Request for FooController
Suggestions:
1) Ensure that you have defined a binding for
IFooService.
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.
Use IoC to resolve instances, but it works only in my HomeController, if I change to another controller using EXACTLY the same code (with the IoC), it generates the error again. Follows the code using the IoC.
using IoC:
private readonly IFooService fooService;
public HomeController()
{
this.fooService = IoC.Instance.Resolve<IFooService>();
}
public ActionResult Index()
{
ViewBag.MyFoos = this.fooService.All();
return View();
}
generates this error:
No matching bindings are available, and the type is not self-bindable.
Error activating IFooService
No matching bindings are available,
and the type is not self-bindable.
Activation path:
1) Request for IFooService
Suggestions:
1) Ensure
that you have defined a binding for IFooService.
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.
Are you sure you have a binding for ISetorService? I don't see one in the code you've posted.
I solved the problem by loading all "NinjectModule" of my application hierarchy.
I thought it was sufficient loading only the main module, then created another statico method within the "NinjectWebCommon" just to separate responsibilities and organizing the code. Below is the code used:
var kernel = new StandardKernel(new Repository(), new Service(), new ValidationAndBusinessRules());
which carry all their Repositories, Services and Validators in creating the Kernel.
private static void RegisterObrigatoryServices(IKernel kernel)
{
kernel.Bind<IIdentityProvider>().To<ServiceIdentityProvider>();
kernel.Bind<Guid>().ToMethod(ctx => default(Guid)).Named("CurrentProcessId");
kernel.Bind<ISession>().ToMethod(ctx =>
{
SessionPoolManager.Update();
Guid processId = kernel.Get<Guid>("CurrentProcessId", new Parameter[] { });
if (processId == default(Guid))
{
return SessionFactoryBuilder.SessionFactory(kernel.Get<IIdentityProvider>()).OpenSession();
}
else
{
ISession session = SessionPoolManager.Get(processId);
if (session == null)
{
session = SessionFactoryBuilder.SessionFactory(kernel.Get<IIdentityProvider>()).OpenSession();
SessionPoolManager.Register(processId, session);
}
return session;
}
});
}
method created by me within the NinjectWebCommon as mentioned above, only to record the required dependencies.
All this code is basically native and has been inserted into the Nuget Ninject.MVC4 package (installed via Package Manager Console within Visual Studio). This package inserts a class in App_Start directory called "NinjectWebCommon," and it is that I made these changes.
the controler is set to send the package documentation, as follows:
public class HomeController : Controller
{
private readonly IFooService fooService;
public HomeController(IFooService fooService)
{
this.fooService = fooService; //Daqui para frente é possível usar normalmente o service.
}
}
I have no idea what is going on with this. It makes no sense to me.
I have a controller that throws the following error:
System.InvalidOperationException: An error occurred when trying to create a controller of type 'LandingController'. Make sure that the controller has a parameterless public constructor. ---> Ninject.ActivationException: Error activating IApiService using binding from IApiService to ApiService No constructor was available to create an instance of the implementation type. Activation path: 2) Injection of dependency IApiService into parameter apiService of constructor of type LandingController 1) Request for LandingController Suggestions: 1) Ensure that the implementation type has a public constructor. 2) If you have implemented the Singleton pattern, use a binding with InSingletonScope() instead.
No matter what I do nothing works.
If I have:
no constructors in the controller
one constructor with the service
two constructors with the service and parameterless
If I hope for the parameterless constructor to work, then it does not resolve the IApiService.
I have the following setup in NinjectWebCommon:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IApiService>().To<ApiService>();
kernel.Bind<IMembersClient>().To<MembersClient>();
}
Controller is:
public class LandingController : Controller
{
IApiService _apiService;
LandingController(IApiService apiService)
{
_apiService = apiService;
}
// GET: Landing
public ActionResult Index()
{
var avm = new ApplicationViewModel();
_apiService.GetAcc();
return View(avm);
}
}
API Service is:
public class ApiService : IApiService
{
private readonly IMembersClient _membersClient;
ApiService(IMembersClient membersClient)
{
_membersClient = membersClient;
}
public void GetAcc()
{
_membersClient.Test();
}
}
Member Client is:
public class MembersClient : IMembersClient
{
public MembersClient()
{
public void Test()
{
}
}
}
This was the best post I found:
Ninject Dependency Injection with Asp.Net MVC3 or MVC4
But it never helped solve the issue.
EDIT: Full NinjectWebCommon
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IApiService>().To<ApiService>();
kernel.Bind<IMembersClient>().To<MembersClient>();
}
EDIT : Trying Property Injection
Code for property injection:
[Inject]
public IApiService ApiServiceC { private get; set; }
Updated Error:
System.InvalidOperationException: An error occurred when trying to create a controller of type 'LandingController'. Make sure that the controller has a parameterless public constructor. ---> Ninject.ActivationException: Error activating IApiService using binding from IApiService to ApiService No constructor was available to create an instance of the implementation type. Activation path: 2) Injection of dependency IApiService into property ApiServiceC of type LandingController 1) Request for LandingController Suggestions: 1) Ensure that the implementation type has a public constructor. 2) If you have implemented the Singleton pattern, use a binding with InSingletonScope() instead.
Well.... after much testing and trying different things.
The solution was to delete IApiService and ApiService completely and recreate them.
That successfully made everything wire up correctly again.
I got this problem with the Controller:
An error occurred when trying to create a controller of type '*.WebMvc.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.
Find the solution for the ApiController, but didn't find anything about normal Controller.
Created new MVC 4 project from scratch.
HomeController.cs:
public class HomeController : Controller
{
private IAccountingUow _uow;
public HomeController(IAccountingUow uow)
{
_uow = uow;
}
UnityDependencyResoler.cs:
public class UnityDependencyResolver : IDependencyResolver
{
private IUnityContainer _container;
public UnityDependencyResolver(IUnityContainer container)
{
_container = container;
RegisterTypes();
}
public object GetService(Type serviceType)
{
try
{
return _container.Resolve(serviceType);
}catch
{
return null;
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return _container.ResolveAll(serviceType);
}catch
{
return null;
}
}
private void RegisterTypes()
{
_container.RegisterType<IAccountingUow, AccountingUow>();
}
}
Global.asax
protected void Application_Start()
{
//Omitted
DependencyResolver.SetResolver( new UnityDependencyResolver( new UnityContainer()));
}
Debugged and found out, that there are even no attempts to resolve IAccountingUow.
What i'm doing wrong?
Thinking about it whole day.
Found where is the issue. Maybe some one will face the same.
The problem is that Unity could not resolve the IAccountingUow, because of hierarchical dependancy on interfaces.
AccountingUow class has two contuctors
public AccountingUow( IRepositoryProvider repositoryProvider)
{
Init(repositoryProvider);
}
public AccountingUow()
{
Init( new RepositoryProvider(new RepositoryFactories()) );
}
Dependency Resolver is not smart enought to take the default parametless contructor. It tries to take interface dependant contructor and fails to resolve it, cause there are no rules for resolving it.
I commented out interface dependant constructor and everything worked fine.
I will post later resolver for the first contructor, maybe someone will use it.
This can also be due to an exception in the parameter-injected constructor of the outer type that is being resolved. The dependencies of that type's constructor might be getting resolved successfully, but if there is an exception in the outer constructor, Unity will report it as "Type Test.Controllers.MyControllerWithInjectedDependencies does not have a default constructor".
Install Nuget Package Unit.WebAP instead of Unity.MVC5 Make sure the correct unity package is installed using nuget
I Installed Unity.MVC5 and was facing similar exception "parameterless constructor"