使用Web API 2加载Ninject组件ICache时出错

时间:2017-02-08 16:07:48

标签: c# asp.net-web-api dependency-injection asp.net-web-api2 ninject

执行web api控制器时收到以下错误。

我收到此错误:

  

加载Ninject组件时出错\ n \ n \ n \ n \ n \ n \ n这个组件已经没有了   在内核的组件容器中注册。

我正在调用的控制器有几个接口,由控制器使用。

我的控制器构造函数:

private ISDKConfiguration iSDKConfiguration;
private ISDKLogin iSDKLogin;
private ISDKLogout iSDKLogout;
private ISDKRetrieveUserSession iSDKRetrieveUserSession;

public SDKAccountController(ISDKConfiguration sdkConfiguration, ISDKLogin sdkLogin, ISDKLogout sdkLogout, ISDKRetrieveUserSession sdkRetrieveUserSession)
{
    iSDKConfiguration = sdkConfiguration;
    iSDKLogin = sdkLogin;
    iSDKLogout = sdkLogout;
    iSDKRetrieveUserSession = sdkRetrieveUserSession;
}

我的HTTP帖子方法

[HttpPost]
[Route("Login")]
public SDKUserDTO Login([FromBody]SDKUserDTO sdkUserDTO)
{
    iSDKConfiguration.Load();

    iSDKLogin.SDKUserDTO = sdkUserDTO;
    iSDKLogin.Process();

    iSDKRetrieveUserSession.SDKUserDTO = sdkUserDTO;
    sdkUserDTO = iSDKRetrieveUserSession.Process();

    return sdkUserDTO;
}

我下载Ninject.Web.Common.WebHost nuget包时包含的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);

            System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.Web.WebApi.NinjectDependencyResolver(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<ISDKConfiguration>().To<SDKConfiguration>();
        kernel.Bind<ISDKLogin>().To<SDKLogin>();
        kernel.Bind<ISDKLogout>().To<SDKLogout>();
        kernel.Bind<ISDKRetrieveUserSession>().To<SDKRetrieveUserSession>();
    }
}

我根据网上的几篇文章编辑了这行代码

System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.Web.WebApi.NinjectDependencyResolver(kernel);

我期待返回以下json。

{
  "UserID": "dhu",
  "Password": "password",
  "Identifier": "LoginTestUsingPostman"
}

但是它抛出了上述异常。

0 个答案:

没有答案