원래 하나의 컨트롤러로 asp.net 웹 API 2 서비스에 대한 DI를 ninject로 설정했으며 모든 것이 올바르게 작동했습니다. 두 번째 컨트롤러를 추가하면 새 컨트롤러에서 ninject가 작동하지 않습니다. 다음 오류가 나타납니다 :Ninject Dependency Injection은 하나의 컨트롤러에서만 작동합니다.
" 'VstsController'형식의 컨트롤러를 만들 때 오류가 발생했습니다. 컨트롤러에 매개 변수가없는 public 생성자가 있는지 확인하십시오."
첫 번째 컨트롤러 (있는 작품을 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에 등록
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>();
}
}
}
:
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에 구성 파일 : VssAuthenticated + IvssAuthenticated뿐만 아니라 코드를 설정 Ninject에 포함 종속성 확인자 :
이 VssConnectionAuthenticated에 있었다 문제는 problem-에 경우 다른 사람에 10namespace 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());
}
}
}
특별히'RepositoryController'를 어디에서 등록 했습니까? 두 클래스가 같은 네임 스페이스에 있습니까? – mason
괜찮아 보이지만 VssConnectionAuthenticated 파일을 다시 한 번 확인할 수 있습니까? –
Ninject를 MVC 및/또는 설치 한 Ninject 특정 패키지에 등록하는 데 사용되는 시작 코드를 표시하십시오. – NightOwl888