zoukankan      html  css  js  c++  java
  • 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();
    

      

  • 相关阅读:
    Scrapy爬虫框架的安装和使用
    pyspider爬虫框架的安装和使用
    Pyquery解析库的安装和使用
    Beautiful Soup解析库的安装和使用
    lxml解析库的安装和使用
    PhantomJS的安装和使用
    GeckoDriver的安装和使用
    用其他主机docker login登录Harbor仓库报错
    强制删除 Terminating 状态的pod
    YUM安装软件提示[Errno 14] curl#6
  • 原文地址:https://www.cnblogs.com/LinFx/p/3121648.html
Copyright © 2011-2022 走看看