0

Iam creating an ASP.NET Web API project with code that used to work in other solution.

I installed Ninject and Ninject.Extensions.ChildKernel packages but they are not found in the file : NinjectResolver.cs

Error : Error

Code :

using Ninject;
using Ninject.Extensions.ChildKernel;
using Assurance.Abstract;
using Assurance.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Dependencies;

namespace Assurance.API
{
    public class NinjectResolver : IDependencyResolver
    {
        private IKernel kernel;

        public NinjectResolver()
            : this(new StandardKernel())
        {

        }

        public NinjectResolver(IKernel ninjectKernel, bool scope = false)
        {
            kernel = ninjectKernel;
            if (!scope)
            {
                AddBindings(kernel);
            }
        }

        public IDependencyScope BeginScope()
        {
            return new NinjectResolver(AddRequestBindings(new ChildKernel(kernel)), true);
        }

        public object GetService(Type serviceType)
        {
            return kernel.TryGet(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return kernel.GetAll(serviceType);
        }

        public void Dispose()
        {

        }

        private void AddBindings(IKernel kernel)
        {
            // singleton and transient bindings go here
        }

        private IKernel AddRequestBindings(IKernel kernel)
        {

            kernel.Bind(typeof(IClientRepository<>)).To(typeof(ClientRepository<>)).InSingletonScope();

            kernel.Bind<IClientService>().To<ClientService>().InSingletonScope();
            return kernel;
        }
    }
}

Ps : the code used to work and I installed succefully the packages bit it always not building.

What Can I do ?

Here is my Nuget manager : Packages installed

  • This may help: https://stackoverflow.com/questions/14016727/how-to-use-ninject-with-asp-net-web-api – Jon Apr 19 '19 at 16:59
  • Also, make sure the version of the .NET Framework that you're compiling for matches the .NET Framework version of the Ninject assemblies. You should also be using the Ninject.Web.WebApi Nuget package – Jon Apr 19 '19 at 17:00
  • @Jon How can I know the versions ? Of .NET framework –  Apr 19 '19 at 17:04
  • 1
    If you look at their changelog on the ninject source repository, they said "Dropped support for .NET Framework 4.5. We now only provide support for the .NET Framework 4.6 and .NET Standard 2.0 target frameworks." You can check your .NET Framework version by going to the Properties of your Project in Solution Explorer, then checking the Target Framework. Make sure it is .NET 4.6 or newer – Jon Apr 19 '19 at 17:06
  • It was a version problem. Solved ! –  Apr 20 '19 at 13:55

0 Answers0