C# 在ninject中将参数传递给接口类
我有一节课要开这样的课C# 在ninject中将参数传递给接口类,c#,asp.net-mvc,asp.net-mvc-3,ninject,C#,Asp.net Mvc,Asp.net Mvc 3,Ninject,我有一节课要开这样的课 public class SessionService : ISession { public HttpContext Context { get; set; } public SessionService(HttpContext context) { this.Context = context; } } private void RegisterDependencyResolver() { var kerne
public class SessionService : ISession
{
public HttpContext Context { get; set; }
public SessionService(HttpContext context)
{
this.Context = context;
}
}
private void RegisterDependencyResolver()
{
var kernel = new StandardKernel();
kernel.Bind<ISession>().To<SessionService>();
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}
我希望能够在MVC3应用程序的不同位置注入会话对象。
我有这个接口
interface ISession
{
HttpContext Context { get; set; }
}
我使用ninject将会话类绑定到如下接口
public class SessionService : ISession
{
public HttpContext Context { get; set; }
public SessionService(HttpContext context)
{
this.Context = context;
}
}
private void RegisterDependencyResolver()
{
var kernel = new StandardKernel();
kernel.Bind<ISession>().To<SessionService>();
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}
private void RegisterDependencyResolver()
{
var kernel=新的标准内核();
kernel.Bind().To();
SetResolver(新的NinjectDependencyResolver(内核));
}
我的问题是如何将Httpcontext参数传递到SessionService构造函数中
非常感谢您的指点
无论您在哪里设置依赖项,都要感谢您:
kernel.Bind<HttpContext>().ToMethod(c => HttpContext.Current);
kernel.Bind().ToMethod(c=>HttpContext.Current);
我有一个bootstrapper类,它通过RegisterServices方法实现这一点:
public static class NinjectMVC3
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule));
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();
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)
{
kernel.Bind<HttpContext>().ToMethod(c => HttpContext.Current);
}
}
公共静态类NinjectMVC3
{
私有静态只读引导程序Bootstrapper=new Bootstrapper();
///
///启动应用程序
///
公共静态void Start()
{
RegisterModule(typeof(OnePerRequestModule));
RegisterModule(typeof(HttpApplicationInitializationModule));
初始化(CreateKernel);
}
///
///停止应用程序。
///
公共静态无效停止()
{
bootstrapper.ShutDown();
}
///
///创建将管理应用程序的内核。
///
///创建的内核。
私有静态IKernel CreateKernel()
{
var kernel=新的标准内核();
注册服务(内核);
返回内核;
}
///
///在这里加载您的模块或注册您的服务!
///
///内核。
私有静态无效注册服务(IKernel内核)
{
kernel.Bind().ToMethod(c=>HttpContext.Current);
}
}