2017-10-06 54 views
0

我最初使用ninject为单个控制器设置了带有asp.net web api 2服务的DI,并且一切正常。添加第二个控制器后,ninject不适用于新的控制器。我收到以下错误:Ninject依赖注入仅适用于一个控制器

“尝试创建类型为'VstsController'的控制器时发生错误,请确保控制器具有无参数公共构造函数。”

首先控制器(用于其ninject作品):

public class RepositoryController : ApiController 
{ 
    private GitHubClient _client; 

    public RepositoryController(IGitHubClientAuthenticated gitHubClientAuthenticated) 
    { 
     _client = gitHubClientAuthenticated.Client; 
     _client.Credentials = gitHubClientAuthenticated.Credentials; 
    } 

第二个控制器:

public class VstsController : ApiController 
{ 
    private VssConnection _connection; 
    public VstsController(IVssConnectionAuthenticated vssConnectionAuthenticated) 
    { 
     _connection = vssConnectionAuthenticated.VssConnection; 
    } 

Ninject配置文件:

private static void RegisterServices(IKernel kernel) 
    { 
     kernel.Bind<IVssConnectionAuthenticated>().To<VssConnectionAuthenticated>(); 
     kernel.Bind<IGitHubClientAuthenticated>().To<GitHubClientAuthenticated>(); 
     kernel.Bind<IAuthenticationHelper>().To<AuthenticationHelper>(); 

    }  

我是否需要调整任何东西,如果我想不断添加控制器?找不到关于此的任何文档。在此先感谢

编辑:包括ninject设置代码,以及VssAuthenticated + IvssAuthenticated:

namespace Dashboard.WebAPI.Models 
{ 
public interface IVssConnectionAuthenticated 
    { 
    VssConnection VssConnection { get; } 
    Uri Uri { get; } 
    } 
} 


namespace Dashboard.WebAPI.Models 
{ 
public class VssConnectionAuthenticated: IVssConnectionAuthenticated 
{ 
    public VssConnection VssConnection { get; private set; } 
    public Uri Uri { get; private set; } 

    VssConnectionAuthenticated() 
    { 
     Uri = new Uri("uri"); 
     string vstsSecretUri = "vstssecreturi"; 
     GetKeyVaultSecret keyVaultSecretGetter = new GetKeyVaultSecret(new AuthenticationHelper(), vstsSecretUri); 
     string keyVaultSecret = keyVaultSecretGetter.KeyVaultSecret; 
     VssBasicCredential vssBasicCredential = new VssBasicCredential(string.Empty, keyVaultSecret); 
     VssConnection = new VssConnection(Uri, vssBasicCredential); 
    } 

完全Ninject配置文件:

namespace Dashboard.WebAPI.App_Start 
{ 
using System; 
using System.Web; 

using Microsoft.Web.Infrastructure.DynamicModuleHelper; 

using Ninject; 
using Ninject.Web.Common; 
using System.Web.Http; 
using Dashboard.WebAPI.Models; 

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 the 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); 
      GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel); 
      return kernel; 
     } 
     catch 
     { 
      kernel.Dispose(); 
      throw; 
     } 
    } 

    /// <summary> 
    /// Load modules and register services 
    /// </summary> 
    /// <param name="kernel">The kernel.</param> 
    private static void RegisterServices(IKernel kernel) 
    { 
     kernel.Bind<IVssConnectionAuthenticated>().To<VssConnectionAuthenticated>(); 
     kernel.Bind<IGitHubClientAuthenticated>().To<GitHubClientAuthenticated>(); 
     kernel.Bind<IAuthenticationHelper>().To<AuthenticationHelper>(); 

    }   
} 

}

注册Ninject作为依赖关系解析器:

namespace Dashboard.WebAPI.App_Start 
{ 
public class NinjectDependencyScope : IDependencyScope 
{ 
    IResolutionRoot resolver; 

    public NinjectDependencyScope(IResolutionRoot resolver) 
    { 
     this.resolver = resolver; 
    } 

    public object GetService(Type serviceType) 
    { 
     if (resolver == null) 
      throw new ObjectDisposedException("this", "This scope has been disposed"); 
     return resolver.TryGet(serviceType); 
    } 

    public System.Collections.Generic.IEnumerable<object> GetServices(Type serviceType) 
    { 
     if (resolver == null) 
      throw new ObjectDisposedException("this", "This scope has been disposed"); 
     return resolver.GetAll(serviceType); 
    } 

    public void Dispose() 
    { 
     IDisposable disposable = resolver as IDisposable; 
     if (disposable != null) 
      disposable.Dispose(); 
     resolver = null; 
    } 
} 
public class NinjectDependencyResolver: NinjectDependencyScope, IDependencyResolver 
{ 
    IKernel kernel; 

    public NinjectDependencyResolver(IKernel kernel) : base(kernel) 
    { 
     this.kernel = kernel; 
    } 

    public IDependencyScope BeginScope() 
    { 
     return new NinjectDependencyScope(kernel.BeginBlock()); 
    } 
} 

}

+0

您是否在任何地方专门注册了'RepositoryController'?两个类都在同一个命名空间中吗? – mason

+0

看起来没问题,但是你能否再次检查VssConnectionAuthenticated文件? –

+0

请显示用于向Ninject注册MVC和/或您安装的Ninject特定软件包的启动代码。 – NightOwl888

回答

1

万一别人运行到这个problem-是VssConnectionAuthenticated问题:构造函数需要是公共的。