如何解决InstancePerHttpRequest?

时间:2014-04-16 11:46:09

标签: c# asp.net-mvc nopcommerce

我正在使用NopCommerce_3.10。我正在尝试使用本教程制作插件:Plugin with data access

当我到达依赖注入步骤时,我在InstancePerHttpRequest()方法上遇到错误。我已经添加了本教程中提到的所有引用。但是我仍然在这个方法中遇到错误。

LookDependencyRegistrar类:

public class LookDependencyRegistrar : IDependencyRegistrar
    {
        private const string CONTEXT_NAME = "nop_object_context_product_look";

        public void Register(ContainerBuilder builder, ITypeFinder typeFinder)
        {
            //Load custom data settings
            var dataSettingsManager = new DataSettingsManager();
            DataSettings dataSettings = dataSettingsManager.LoadSettings();

            //Register custom object context
            builder.Register<IDbContext>(c => RegisterIDbContext(c, dataSettings)).Named<IDbContext>(CONTEXT_NAME).InstancePerHttpRequest();
            builder.Register(c => RegisterIDbContext(c, dataSettings)).InstancePerHttpRequest();

            //Register services
            builder.RegisterType<LookService>().As<ILookService>();

            //Override the repository injection
            builder.RegisterType<EfRepository<Look>>().As<IRepository<Look>>().WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT_NAME)).InstancePerHttpRequest();
        }

        public int Order
        {
            get { return 0; }
        }

        /// <summary>
        /// Registers the I db context.
        /// </summary>
        /// <param name="componentContext">The component context.</param>
        /// <param name="dataSettings">The data settings.</param>
        /// <returns></returns>
        private LookObjectContext RegisterIDbContext(IComponentContext componentContext, DataSettings dataSettings)
        {
            string dataConnectionStrings;

            if (dataSettings != null && dataSettings.IsValid())
            {
                dataConnectionStrings = dataSettings.DataConnectionString;
            }
            else
            {
                dataConnectionStrings = componentContext.Resolve<DataSettings>().DataConnectionString;
            }

            return new LookObjectContext(dataConnectionStrings);
        }
    }

InstancePerHttpRequest()方法的错误消息:

enter image description here

如何在InstancePerHttpRequest()方法中解决错误?

2 个答案:

答案 0 :(得分:1)

尝试替换此行:

builder.RegisterType<LookService>().As<ILookService>();

替换为

builder.RegisterType<LookService>().As<ILookService>().InstancePerHttpRequest();

答案 1 :(得分:1)

InstancePerHttpRequest是驻留在另一个命名空间/程序集中的扩展方法。您应该导入以下命名空间以进行正确编译:

using Autofac.Integration.Mvc;