Autofac不能正确处理对象

时间:2013-04-11 05:13:03

标签: asp.net-mvc memory-leaks autofac

我确信我没有正确设置autofac。我的应用程序内存消耗徘徊在1.5gb左右,几乎没有任何用户。我对内存使用的来源感到很失落。所以我将发布几乎所有的DI配置。

        public virtual void Register(ContainerBuilder builder)
    {
        //data layer
        var dataSettingsManager = new DataSettingsManager();
        var dataProviderSettings = dataSettingsManager.LoadSettings();
        builder.Register(c => dataSettingsManager.LoadSettings()).As<DataSettings>();
        builder.Register(x => new EfDataProviderManager(x.Resolve<DataSettings>())).As<BaseDataProviderManager>().InstancePerDependency();
        builder.Register(x => (IEfDataProvider)x.Resolve<BaseDataProviderManager>().LoadDataProvider()).As<IEfDataProvider>().InstancePerDependency();

        if (dataProviderSettings != null && dataProviderSettings.IsValid())
        {
            var efDataProviderManager = new EfDataProviderManager(dataSettingsManager.LoadSettings());
            var dataProvider = (IEfDataProvider)efDataProviderManager.LoadDataProvider();
            dataProvider.InitConnectionFactory();

            builder.Register<IDbContext>(c => new CellObjectContext(dataProviderSettings.DataConnectionString)).InstancePerHttpRequest();
        }
        else
        {
            throw new Exception("ConnectionString in the web.config/app.config hasn't been defined");
        }

        builder.Register(c => (new HttpContextWrapper(HttpContext.Current) as HttpContextBase))
            .As<HttpContextBase>().InstancePerHttpRequest();


        //helpers
        builder.RegisterType<StoreOrderHelper>().As<IStoreOrderHelper>().InstancePerHttpRequest();
        builder.RegisterType<GiftCardHelper>().As<IGiftCardHelper>().InstancePerHttpRequest();
        builder.RegisterType<ProductHelper>().As<IProductHelper>().InstancePerHttpRequest();
        builder.RegisterType<ProspectHelper>().As<IProspectHelper>().InstancePerHttpRequest();
        builder.RegisterType<OfferProductHelper>().As<IOfferProductHelper>().InstancePerHttpRequest();
        builder.RegisterType<CustomerHelper>().As<ICustomerHelper>().InstancePerHttpRequest();
        builder.RegisterType<CartHelper>().As<ICartHelper>().InstancePerHttpRequest();

        //cache
        builder.RegisterType<MemcachedClient>().As<IMemcachedClient>().SingleInstance();
        builder.RegisterType<MemoryCacheManager>().As<ICacheManager>().Named<ICacheManager>("cache_static").SingleInstance();
        builder.RegisterType<MemcachedManager>().As<ICacheManager>().Named<ICacheManager>("cache_static_mem").InstancePerHttpRequest();
        builder.RegisterType<RequestCacheManager>().As<ICacheManager>().Named<ICacheManager>("cache_per_request").InstancePerHttpRequest();

        //repository
        builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).InstancePerHttpRequest();

        //services
        builder.RegisterType<SettingService>().As<ISettingService>()
            .InstancePerHttpRequest();
        builder.RegisterType<ContentService>().As<IContentService>().InstancePerHttpRequest();
        builder.RegisterType<StoreOrderService>().As<IStoreOrderService>()
            .WithParameter(ResolvedParameter.ForNamed<ICacheManager>("cache_per_request"))
            .InstancePerHttpRequest();
        builder.RegisterType<OfferProductService>().As<IOfferProductService>()
            .InstancePerHttpRequest();
        builder.RegisterType<OfferProductService>().As<IOfferProductService>()
         .InstancePerHttpRequest().PropertiesAutowired(PropertyWiringFlags.AllowCircularDependencies);
        builder.RegisterType<ProductService>().As<IProductService>()
            .InstancePerHttpRequest();
        builder.RegisterType<SubstituteService>().As<ISubstituteService>().InstancePerHttpRequest();

        builder.RegisterType<ErrorService>().As<IErrorService>()
            .InstancePerHttpRequest();

       builder.RegisterType<BonusService>().As<IBonusService>().InstancePerHttpRequest()
           .InstancePerHttpRequest();

        builder.RegisterType<TastingNoteService>().As<ITastingNoteService>().InstancePerHttpRequest()
            .InstancePerHttpRequest();

        builder.RegisterType<OfferProductAdminService>().As<IOfferProductAdminService>().InstancePerHttpRequest();
        builder.RegisterType<ResourceService>().As<IResourceService>().InstancePerHttpRequest();
        builder.RegisterType<FeedProductService>().As<IFeedProductService>().InstancePerHttpRequest();
        builder.RegisterType<ClubService>().As<IClubService>().InstancePerHttpRequest();

        builder.RegisterType<CasePricingService>().As<ICasePricingService>().InstancePerHttpRequest()
            .InstancePerHttpRequest();

        builder.RegisterType<StoreDiscountService>().As<IStoreDiscountService>().InstancePerHttpRequest()
            .InstancePerHttpRequest();

        builder.RegisterType<MixComponentService>().As<IMixComponentService>().InstancePerHttpRequest();
        builder.RegisterType<BrandService>().As<IBrandService>().InstancePerHttpRequest();

        builder.RegisterType<CardTypeService>().As<ICardTypeService>()
            .InstancePerHttpRequest();


        builder.RegisterType<CategoryService>().As<ICategoryService>()
            .InstancePerHttpRequest();

        builder.RegisterType<ExcludedOfferProductService>().As<IExcludedOfferProductService>()
            .InstancePerHttpRequest();            

        builder.RegisterType<CustomerTitleService>().As<ICustomerTitleService>().InstancePerHttpRequest();
        builder.RegisterType<DeliveryInstructionsOptionService>().As<IDeliveryInstructionsOptionService>().InstancePerHttpRequest();        

        builder.RegisterType<SuburbService>().As<ISuburbService>().InstancePerHttpRequest();
        builder.RegisterType<PayPalOrderService>().As<IPayPalOrderService>().InstancePerHttpRequest();
        builder.RegisterType<ProspectInterface>().As<IProspectInterface>().InstancePerHttpRequest(); ;

        builder.RegisterType<CreditCardInterface>().As<ICreditCardInterface>().InstancePerHttpRequest();
        builder.RegisterType<CreditCardHelper>().As<ICreditCardHelper>().InstancePerHttpRequest();

        builder.RegisterType<GiftRecipientInterface>().As<IGiftRecipientInterface>().InstancePerHttpRequest();
        builder.RegisterType<GiftRecipientHelper>().As<IGiftRecipientHelper>().InstancePerHttpRequest();

        //register controllers

    }

在我的全局asax中,我在Application_Start上有以下内容

var builder = new ContainerBuilder();

        DependencyRegistrar dr = new DependencyRegistrar();
        dr.Register(builder);
        builder.RegisterControllers(AppDomain.CurrentDomain.GetAssemblies());

        IContainer c = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(c));
        _containerProvider = new ContainerProvider(c);

我在Application_BeginRequest或Application_EndRequest中没有代码,也没有服务/存储库/帮助程序实现IDisposable。这个配置有问题吗?

0 个答案:

没有答案