NoConstructorsFoundException: No accessible constructors were found for the type 'Nop.Data.IRepository...

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
3 years ago
Working on upgrading a plugin that works with NopCommerce 3.8 to support 4.3. The service in this plugin uses a field of type IRepositoy<MyCustomEntity>. This field is supposed to be populated with data through dependency injection as is standard throughout NopCommerce. In this case, MyCustomEntity comes from a SQL Server table of the same name (dbo.MyCustomEntity). For whatever reason, the dependency injection is not working for this. It either goes through as null if I don't include it in the DependencyRegistrar, or it throws an error if I do.

I have compared my setup to the other plugins that came out of the box and it looks the same from what I can see. First I have a model that looks like this:

public partial class MyCustomEntity: BaseEntity
    {
        public int ProductId { get; set; }
        
        public bool Status { get; set; }
        
        public bool IsDeleted { get; set; }

        public DateTime? InTime { get; set; }

        public int StoreId { get; set; }

        public int LanguageId { get; set; }
    
    }

Each of these properties correspond to a column in the SQL Server table.

Then it is used in the service like this:

public partial class MyCustomService: IMyCustomService
    {
...
private readonly IRepository<MyCustomEntity> _myCustomEntityRepository;
...
public MyCustomService(IRepository<MyCustomEntity> myCustomEntityRepository)
        {
            _myCustomEntityRepository = myCustomEntityRepository;
        }
...}


And in the DependencyRegistrar, the service is registered like so:

public void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
        {
...
builder.RegisterType<MyCustomService>().As<IMyCustomService>().InstancePerLifetimeScope();
}


With this setup, the myCustomEntityRepository comes through as NULL.

Now in 3.8, along with the service, this custom entity would also be registered here in the DependencyRegistrar, but I have noticed now in 4.3, the other plugins do not seem to be doing this anymore, they appear to just be magically working without it. Nevertheless, I tried adding it in anyway like before:

builder.RegisterType<IRepository<MyCustomEntity>>()
               .As<IRepository<MyCustomEntity>>()
               .WithParameter(ResolvedParameter.ForNamed<INopDataProvider>("nop_object_context_my_custom_entity"))
               .InstancePerLifetimeScope();


but then this throws the following error on startup:

NoConstructorsFoundException: No accessible constructors were found for the type 'Nop.Data.IRepository`1[[My.Plugin.Name.MyCustomEntity, My.Plugin.Name, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'.


Not sure why this isn't working, or how Nop knows how to populate the IRepository of other plugins without explicitly registering them. Been stuck on this for a while so any help is appreciated. Thanks!
3 years ago
I was able to resolve the issue by following the documentation on "Plugin with data access":

https://docs.nopcommerce.com/en/developer/plugins/plugin-with-data-access.html

The pieces I was missing that are new were the *Builder and SchemaMigration classes. This seems to be what actually maps the entity to the database table.
3 years ago
Thanks for posting your solution here.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.