亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

NopCommerce 4.20 插件開發錯誤與依賴注入

NopCommerce 4.20 插件開發錯誤與依賴注入

C#
Helenr 2023-07-09 09:57:28
我有一個帶有 dBContext 名稱BookAppointmentDBContext和依賴項注冊器的 NopCommerce 插件開發DependencyRegistrar,請參閱下面的代碼片段。public class DependencyRegistrar : IDependencyRegistrar{    private const string CONTEXT_NAME ="nop_object_context_bookappointment";    public  void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)    {        builder.RegisterType<BookAppointmentService>().As<IBookAppointmentService>().InstancePerLifetimeScope();        //data context        builder.RegisterPluginDataContext<BookAppointmentDBContext>(CONTEXT_NAME);        //override required repository with our custom context        builder.RegisterType<EfRepository<CarInspectionModel>>()            .As<IRepository<CarInspectionModel>>()            .WithParameter(ResolvedParameter.ForNamed<IDbContext>(CONTEXT_NAME))            .InstancePerLifetimeScope();    }    public int Order => 1;}和下面的 BookAppointmentDBContext 類public class BookAppointmentDBContext : DbContext, IDbContext{    #region Ctor    public BookAppointmentDBContext(DbContextOptions<BookAppointmentDBContext> options) : base(options)    {    }  /*the other implementation of IDbContext as found in http://docs.nopcommerce.com/display/en/Plugin+with+data+access*/}另外,我有一個 BasePluglin 類public class BookAppointmentPlugin : BasePlugin{    private IWebHelper _webHelper;    private readonly BookAppointmentDBContext _context;    public BookAppointmentPlugin(IWebHelper webHelper, BookAppointmentDBContext context)    {        _webHelper = webHelper;        _context = context;    }    public override void Install()    {        _context.Install();        base.Install();    }    public override void Uninstall()    {        _context.Uninstall();        base.Uninstall();    }}我已經BookAppointmentDBContext注冊了,但錯誤狀態相反。知道我做錯了什么嗎?
查看完整描述

1 回答

?
浮云間

TA貢獻1829條經驗 獲得超4個贊

這個問題是缺少注冊的,DbContextOption它是初始化目標數據庫上下文所需的構造函數的一部分。


在內部就是這樣RegisterPluginDataContext做的。


/// <summary>

/// Represents extensions of Autofac ContainerBuilder

/// </summary>

public static class ContainerBuilderExtensions

{

? ? /// <summary>

? ? /// Register data context for a plugin

? ? /// </summary>

? ? /// <typeparam name="TContext">DB Context type</typeparam>

? ? /// <param name="builder">Builder</param>

? ? /// <param name="contextName">Context name</param>

? ? public static void RegisterPluginDataContext<TContext>(this ContainerBuilder builder, string contextName) where TContext : DbContext, IDbContext

? ? {

? ? ? ? //register named context

? ? ? ? builder.Register(context => (IDbContext)Activator.CreateInstance(typeof(TContext), new[] { context.Resolve<DbContextOptions<TContext>>() }))

? ? ? ? ? ? .Named<IDbContext>(contextName).InstancePerLifetimeScope();

? ? }

}

DbContextOptions<TContext>請注意,它在激活上下文時嘗試解析。


您需要構建數據庫上下文選項并將其提供給容器,以便在解析時可以將其注入到上下文中。


private const string CONTEXT_NAME ="nop_object_context_bookappointment";

public? void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config) {


? ? //...code removed for brevity


? ? var optionsBuilder = new DbContextOptionsBuilder<BookAppointmentDBContext>();

? ? optionsBuilder.UseSqlServer(connectionStringHere);

? ? DbContextOptions<BookAppointmentDBContext> options = optionsBuilder.Options;

? ? builder.RegisterInstance<DbContextOptions<BookAppointmentDBContext>>(options);?


? ? //data context

? ? builder.RegisterPluginDataContext<BookAppointmentDBContext>(CONTEXT_NAME);


? ? //...code removed for brevity

}

查看完整回答
反對 回復 2023-07-09
  • 1 回答
  • 0 關注
  • 132 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號