我正在尝试使用 Bootstrapper 为我的应用程序、ioc、automapper、配置等进行初始化。
我需要一些关于如何使用引导程序在 asp.net webapi 中正确设置 ninject 的指导。使用以下配置,我的 apicontroller 无法解析 IMyService 依赖项。看起来它正在使用不同的ninject内核。
我的 NinjectWebCommon
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();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
GlobalConfiguration.Configuration.DependencyResolver = new MyResolver(kernel);
RegisterServices(kernel);
return kernel;
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
}
}
引导程序 ninject 注册
public class DIRegistration : INinjectRegistration
{
public void Register(IKernel container)
{
container.Bind<IMyService>().To<MyService>().InTransientScope();
}
}
public class MyService: IMyService
{
public string GetString()
{
return "My String!!!!!";
}
}
public interface IMyService
{
string GetString();
}