• mvc4 ninject 注入HttpContext


    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();
    
            /// <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>();
    
                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)
            {
                var aa = new HttpContextWrapper(HttpContext.Current) as HttpContextBase;
                kernel.Bind<HttpContext>().ToMethod(ctx => HttpContext.Current).InRequestScope();
                kernel.Bind<HttpContextBase>().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<HttpContextBase>()
                //    .InstancePerHttpRequest();
                //var httpContext = new FakeHttpContext("~/") as HttpContextBase;
    
                kernel.Bind<HttpContextBase>().ToMethod(ctx => HttpContext.Current != null
                                                            ? (new HttpContextWrapper(HttpContext.Current) as
                                                               HttpContextBase)
                                                            : (new LinFx.Web.Fakes.FakeHttpContext("~/") as HttpContextBase));
    
                kernel.Bind<IDbContext>().To<YlsPayContext>().InRequestScope();
                kernel.Bind<DbContext>().To<YlsPayContext>().InRequestScope();
    
                kernel.Bind<ShopService>().ToSelf().InRequestScope();
                kernel.Bind<ProductService>().ToSelf().InRequestScope();
                kernel.Bind<OrderService>().ToSelf().InRequestScope();
                kernel.Bind<CategoryService>().ToSelf().InRequestScope();
    
                kernel.Bind<ICacheManager>().To<MemoryCacheManager>().InRequestScope();
                kernel.Bind<IWorkContext>().To<WorkContext>().InSingletonScope();
                kernel.Bind<IUserService>().To<UserService>().InRequestScope();
                kernel.Bind<IProductService>().To<ProductService>().InRequestScope();
                //kernel.Bind<IAddressService>().To<ProductService>().InRequestScope();
    
                kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>)).InRequestScope(); ;
    
                //search
                kernel.Bind<IIndexManager>().To<DefaultIndexManager>();
                kernel.Bind<ISearchService>().To<SearchService>();
    
                kernel.Bind<IIndexProvider>().To<LuceneIndexProvider>();
                kernel.Bind<ISearchBuilder>().To<LuceneSearchBuilder>();
            }        
        }
    }

    引用

    Ninject

    Ninject.Web.Common

    Ninject.Web.Mvc

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

    这里出问题了吗?

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

      

  • 相关阅读:
    建立自己的影响力
    在病房垒代码
    知乎确实不错
    不在乎你用到了什么技术,而在于你提供了哪些有价值的东西
    oschina上有不少好的项目
    我为何坚守Java?
    掌握了学习方法才能学到更多知识
    Jrebel实现Jetty 热部署
    互联网到底能干什么?我们还能干些什么?
    centos 阿里云 安装VNC Viewer
  • 原文地址:https://www.cnblogs.com/LinFx/p/3121648.html
Copyright © 2020-2023  润新知