2017-10-06 7 views
0

私はもともと単一のコントローラでasp.net web api 2サービスのためにninjectでDIをセットアップしていましたが、すべて正常に動作していました。 2台目のコントローラを追加すると、新しいコントローラは動作しません。Ninject Dependency Injectionは1つのコントローラでしか動作しません

"タイプ '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>(); 

    }  

私が欲しい場合、私は何を微調整する必要がありますかコントローラを追加し続けるには?これに関する文書は見つかりませんでした。事前に感謝

EDIT

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設定ファイル::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などVssAuthenticated + IvssAuthenticated含みます依存関係リゾルバー:

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

これは問題がVssConnectionAuthenticatedにあったproblem-に場合、誰が実行されます。コンストラクタは、公開する必要があります。

関連する問題