C# Ninject 绑定导致标准 MVC class AccountController 中的空存储库
C# Ninject binding results in empty repository in standard MVC class AccountController
我正在使用 C# 中的 MVC 框架制作应用程序。
我们想创建一个包含我们所有用户的存储库 'uitlenerRepository'。
我使用 RegisterServices NinjectWebCommon 中的 Ninject 绑定了存储库 class:
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<IMateriaalRepository>().To<MateriaalRepository>().InRequestScope();
kernel.Bind<IReservatieRepository>().To<ReservatieRepository>().InRequestScope();
kernel.Bind<IUitlenerRepository>().To<UitlenerRepository>();
kernel.Bind<UitleenAppContext>().ToSelf().InRequestScope();
}
}
如您所见,我对其他存储库尝试了相同的方法,并取得了成功。
其他存储库之间的唯一区别是我在预定义的 class 'AccountController' 中创建了一个新存储库。因为在 class 中,我可以轻松添加新用户。在此 class 我创建了一个空构造函数并将其添加为第三个参数。
public class AccountController : Controller
{
private ApplicationSignInManager _signInManager;
private ApplicationUserManager _userManager;
private IUitlenerRepository uitlenerRepository;
public AccountController()
{
}
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, IUitlenerRepository uitlenerRepository)
{
UserManager = userManager;
SignInManager = signInManager;
this.uitlenerRepository = uitlenerRepository;
}
我们无法向存储库添加新用户。调试的时候发现repository的值为null,说明绑定不正常
有谁知道如何解决这个问题?
正在使用 AccountController()
构造函数,这就是它为空的原因。 Ninject 将选择它可以解析的参数最多的构造函数。我怀疑 ApplicationUserManager
或 ApplicationSignInManager
需要绑定。我建议您删除 AccountController()
构造函数 - 没有任何参数的构造函数 - 然后 ninject 将抛出异常并告诉您它不能使用另一个构造函数的确切原因。
我正在使用 C# 中的 MVC 框架制作应用程序。 我们想创建一个包含我们所有用户的存储库 'uitlenerRepository'。
我使用 RegisterServices NinjectWebCommon 中的 Ninject 绑定了存储库 class:
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<IMateriaalRepository>().To<MateriaalRepository>().InRequestScope();
kernel.Bind<IReservatieRepository>().To<ReservatieRepository>().InRequestScope();
kernel.Bind<IUitlenerRepository>().To<UitlenerRepository>();
kernel.Bind<UitleenAppContext>().ToSelf().InRequestScope();
}
}
如您所见,我对其他存储库尝试了相同的方法,并取得了成功。 其他存储库之间的唯一区别是我在预定义的 class 'AccountController' 中创建了一个新存储库。因为在 class 中,我可以轻松添加新用户。在此 class 我创建了一个空构造函数并将其添加为第三个参数。
public class AccountController : Controller
{
private ApplicationSignInManager _signInManager;
private ApplicationUserManager _userManager;
private IUitlenerRepository uitlenerRepository;
public AccountController()
{
}
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, IUitlenerRepository uitlenerRepository)
{
UserManager = userManager;
SignInManager = signInManager;
this.uitlenerRepository = uitlenerRepository;
}
我们无法向存储库添加新用户。调试的时候发现repository的值为null,说明绑定不正常
有谁知道如何解决这个问题?
正在使用 AccountController()
构造函数,这就是它为空的原因。 Ninject 将选择它可以解析的参数最多的构造函数。我怀疑 ApplicationUserManager
或 ApplicationSignInManager
需要绑定。我建议您删除 AccountController()
构造函数 - 没有任何参数的构造函数 - 然后 ninject 将抛出异常并告诉您它不能使用另一个构造函数的确切原因。