Ninject 没有解决控制器的依赖关系

Ninject not resolving dependencies for a Controller

我不知道这是怎么回事。这对我来说毫无意义。

我有一个控制器抛出以下错误:

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.

无论我做什么都无济于事。 如果我有:

如果我希望无参数构造函数工作,那么它不会解析 IApiService。

我在 NinjectWebCommon 中有以下设置:

private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<IApiService>().To<ApiService>();
    kernel.Bind<IMembersClient>().To<MembersClient>();
}  

控制器是:

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 服务是:

public class ApiService : IApiService
{
    private readonly IMembersClient _membersClient;

    ApiService(IMembersClient membersClient)
    {
        _membersClient = membersClient;
    }

    public void GetAcc()
    {
        _membersClient.Test();
    }
}

会员客户是:

public class MembersClient : IMembersClient 
{
    public MembersClient()
    {
      public void Test()
      {

      }
    }
}

这是我发现的最好的 post:

Ninject Dependency Injection with Asp.Net MVC3 or MVC4

但这并没有帮助解决问题。

编辑:完整的 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>();
    }

编辑:尝试属性注入

属性 注入代码:

[Inject]
public IApiService ApiServiceC { private get; set; }

更新错误:

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.

嗯....经过多次测试和尝试不同的东西。

解决方案是完全删除 IApiService 和 ApiService 并重新创建它们。

这成功地使一切重新正确连接。