NopCommerce 4.20 依赖注入插件开发错误
NopCommerce 4.20 Plugin development error with Dependency Injection
我有一个 NopCommerce 插件开发,其名称为 dBContext BookAppointmentDBContext
并且 Dependency Registrar 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 class
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 class 和
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();
}
}
我一直遇到这个错误:
ComponentNotRegisteredException: The requested service 'Microsoft.EntityFrameworkCore.DbContextOption 1[[Nop.Plugin.Misc.BookAppointment.Models.BookAppointmentDBContext, Nop.Plugin.Misc.BookAppointment, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.
我已 BookAppointmentDBContext
注册,但错误状态并非如此。
知道我做错了什么吗?
此问题是缺少注册的 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
}
我有一个 NopCommerce 插件开发,其名称为 dBContext BookAppointmentDBContext
并且 Dependency Registrar 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 class
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 class 和
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();
}
}
我一直遇到这个错误:
ComponentNotRegisteredException: The requested service 'Microsoft.EntityFrameworkCore.DbContextOption 1[[Nop.Plugin.Misc.BookAppointment.Models.BookAppointmentDBContext, Nop.Plugin.Misc.BookAppointment, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.
我已 BookAppointmentDBContext
注册,但错误状态并非如此。
知道我做错了什么吗?
此问题是缺少注册的 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
}