Plugin Development

8 months ago
I am writing a custom plugin and slowly try to extend its functionality.

This constructor works great
public BlankObjectContext(DbContextOptions<BlankObjectContext> options) : base(options)
        {
        }



This overloaded constructor
public OverloadObjectContext(DbContextOptions<OverloadObjectContext> options, IDbContext dbContext, INopFileProvider fileProvider) : base(options)
        {
            this._dbContext = dbContext;
            this._fileProvider = fileProvider;
        }


Gives me an error at
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();
        }


System.MissingMethodException: 'Constructor on type 'Nop.Plugin.Fanolli.Journal.Data.OverloadObjectContext' not found.'

any idea why?
8 months ago
I think it could be that your Activator.CreateInstance() parameter   new[] { ... }   is an array of only one object, and your overloaded constructor has four parameters.  I.e. it can't find a matching constructor.
8 months ago
you are right...

I ended up doing it like this
public MyPluginObjectContext(DbContextOptions<MyPluginObjectContext> options) : base(options)
        {
            _dbContext = EngineContext.Current.Resolve<IDbContext>();
            _fileProvider = EngineContext.Current.Resolve<INopFileProvider>();
        }