博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mvc4 ninject 注入HttpContext
阅读量:4681 次
发布时间:2019-06-09

本文共 3735 字,大约阅读时间需要 12 分钟。

using LinFx.Data;using LinFx.Index;using LinFx.Plugin.Caching;using LinFx.Plugin.Lucene.Services;using LinFx.Plugin.Search.Services;using LinFx.Security;using LinFx.Web;using YLSPay.Data;using YLSPay.Data.Service;[assembly: WebActivator.PreApplicationStartMethod(typeof(YLSPay.App_Start.NinjectWebCommon), "Start")][assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(YLSPay.App_Start.NinjectWebCommon), "Stop")]namespace YLSPay.App_Start{    using System;    using System.Web;    using Microsoft.Web.Infrastructure.DynamicModuleHelper;    using Ninject;    using Ninject.Web.Common;    using System.Data.Entity;    public static class NinjectWebCommon     {        static readonly Bootstrapper bootstrapper = new Bootstrapper();        ///         /// Starts the application        ///         public static void Start()         {            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));            bootstrapper.Initialize(CreateKernel);        }                ///         /// Stops the application.        ///         public static void Stop()        {            bootstrapper.ShutDown();        }                ///         /// Creates the kernel that will manage your application.        ///         /// 
The created kernel.
private static IKernel CreateKernel() { var kernel = new StandardKernel(); kernel.Bind
>().ToMethod(ctx => () => new Bootstrapper().Kernel); kernel.Bind
().To
(); RegisterServices(kernel); return kernel; } ///
/// Load your modules or register your services here! /// ///
The kernel. private static void RegisterServices(IKernel kernel) { var aa = new HttpContextWrapper(HttpContext.Current) as HttpContextBase; kernel.Bind
().ToMethod(ctx => HttpContext.Current).InRequestScope(); kernel.Bind
().ToMethod(ctx => new HttpContextWrapper(HttpContext.Current)).InRequestScope(); //kernel.Register(c => // //register FakeHttpContext when HttpContext is not available // HttpContext.Current != null ? // (new HttpContextWrapper(HttpContext.Current) as HttpContextBase) : // (new FakeHttpContext("~/") as HttpContextBase)) // .As
() // .InstancePerHttpRequest(); //var httpContext = new FakeHttpContext("~/") as HttpContextBase; kernel.Bind
().ToMethod(ctx => HttpContext.Current != null ? (new HttpContextWrapper(HttpContext.Current) as HttpContextBase) : (new LinFx.Web.Fakes.FakeHttpContext("~/") as HttpContextBase)); kernel.Bind
().To
().InRequestScope(); kernel.Bind
().To
().InRequestScope(); kernel.Bind
().ToSelf().InRequestScope(); kernel.Bind
().ToSelf().InRequestScope(); kernel.Bind
().ToSelf().InRequestScope(); kernel.Bind
().ToSelf().InRequestScope(); kernel.Bind
().To
().InRequestScope(); kernel.Bind
().To
().InSingletonScope(); kernel.Bind
().To
().InRequestScope(); kernel.Bind
().To
().InRequestScope(); //kernel.Bind
().To
().InRequestScope(); kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>)).InRequestScope(); ; //search kernel.Bind
().To
(); kernel.Bind
().To
(); kernel.Bind
().To
(); kernel.Bind
().To
(); } }}

 

引用

Ninject

Ninject.Web.Common

Ninject.Web.Mvc

 

不知道为什么,,我总不能成功.

这里出问题了吗?

kernel.Bind
().ToMethod(ctx => HttpContext.Current).InRequestScope(); kernel.Bind
().ToMethod(ctx => new HttpContextWrapper(HttpContext.Current)).InRequestScope();

  

 

转载于:https://www.cnblogs.com/LinFx/archive/2013/06/06/3121648.html

你可能感兴趣的文章
linux网卡缓冲区设置,【Linux】tcp缓冲区大小的默认值、最大值
查看>>
opus编译linux,Linux 下源码编译FFMEG
查看>>
linux 运行real basic,REALbasic 快速入门.pdf
查看>>
linux启动tomcat不停的触发gc,tomcat启动时就频繁gc和full gc
查看>>
linux uart串口驱动,X-017-KERNEL-串口驱动开发之uart driver框架
查看>>
linux 添加串口数量,如何在Linux中添加4个以上的串口设备?
查看>>
关于sqoop导入数据的时候添加--split-by配置项对sqoop的导入速度的影响。
查看>>
nginx配置
查看>>
2014-11-9------- 设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。...
查看>>
python 魔法方法补充(__setattr__,__getattr__,__getattribute__)
查看>>
NOIP 2010 关押罪犯
查看>>
CentOS7.5删除旧的内核
查看>>
Java常用的非受检异常
查看>>
HDOJ-2054
查看>>
centos7安装eclipse
查看>>
Web:AJAX的详解
查看>>
两种比较器Comparable 和 Comparator
查看>>
S2JDBC テーブルを利用した独自仕様のid採番メソッド
查看>>
P3698 [CQOI2017]小Q的棋盘
查看>>
动态规划入门 洛谷P2409 Y的积木
查看>>