nop 4.50 plugin autofac error

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
1 ano atrás
I'm adding a new plugin with data access to a new table. I've followed the doc at https://docs.nopcommerce.com/en/developer/plugins/plugin-with-data-access.html

I get an Autofac error that my plugin's serivce's constructor can't be found at run time.

Autofac.Core.DependencyResolutionException: An exception was thrown while activating WZ.Plugin.Widgets.SKUAllocationByOwner.Controllers.SKUAllocationController -> WZ.Plugin.Widgets.SKUAllocationByOwner.Factories.SKUAllocationModelFactory.   ---> Autofac.Core.DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'WZ.Plugin.Widgets.SKUAllocationByOwner.Factories.SKUAllocationModelFactory' can be invoked with the available services and parameters:  Cannot resolve parameter 'WZ.Plugin.Widgets.SKUAllocationByOwner.Services.SKUAllocationServic <snip>

Here is my plugin starup:
 public class PluginNopStartup : INopStartup
    {
        public virtual void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            services.Configure<RazorViewEngineOptions>(options =>
            {
                options.ViewLocationExpanders.Add(new ViewLocationExpander());
            });        

            //register services and interfaces
            services.AddScoped<ISKUAllocationModelFactory, SKUAllocationModelFactory>();
            services.AddScoped<ISKUAllocationService, SKUAllocationService>();                      
            services.AddScoped<IWZImportManager, WZImportManager>();
        }

        public void Configure(IApplicationBuilder application)
        {
        }

        public int Order => 3000;
    }

Model:
   public record SKUAllocationModel : BaseNopEntityModel
    {          
        public string OwnerName { get; set; }    
        public string Sku { get; set; }
        public int AllocationQuantity { get; set; }
        public int UsedAllocationQuantity { get; set; }
        public DateTime FromDateTime { get; set; }
        public DateTime ToDateTime { get; set; }
}


Domain:

public partial class SkuAllocation : BaseEntity
    {            
        public string OwnerName { get; set; }
        public string Sku { get; set; }
        public int AllocationQuantity { get; set; }
        public int UsedAllocationQuantity { get; set; }
        public DateTime FromDateTime { get; set; }
        public DateTime ToDateTime { get; set; }        
    }  


Plugin Builder:

public class PluginBuilder : NopEntityBuilder<SkuAllocation>
    {
        public override void MapEntity(CreateTableExpressionBuilder table)
        {
            table
               .WithColumn(nameof(SKUAllocationModel.Id)).AsInt32().PrimaryKey()
               .WithColumn(nameof(SKUAllocationModel.OwnerName)).AsString(400).NotNullable()
               .WithColumn(nameof(SKUAllocationModel.Sku)).AsString(400).NotNullable()
               .WithColumn(nameof(SKUAllocationModel.AllocationQuantity)).AsInt32().NotNullable()
               .WithColumn(nameof(SKUAllocationModel.UsedAllocationQuantity)).AsInt32().NotNullable()
               .WithColumn(nameof(SKUAllocationModel.FromDateTime)).AsDateTime().NotNullable()
               .WithColumn(nameof(SKUAllocationModel.ToDateTime)).AsDateTime().NotNullable();                
        }    
    }

service:

public class SKUAllocationService : ISKUAllocationService
    {              
        private readonly IRepository<SkuAllocation> _skuAllocationRepository;
        private readonly ILogger _logger;
    
        public SKUAllocationService(
            IRepository<SkuAllocation> skuAllocationRepository, ILogger logger)
        {            
            _skuAllocationRepository = skuAllocationRepository;
            _logger = logger;
        }
<more methods snipped>


Model Factory:

public partial class SKUAllocationModelFactory :ISKUAllocationModelFactory
    {

        private readonly IWorkContext _workContext;
        private readonly VendorSettings _vendorSettings;
        private readonly SKUAllocationService _skuAllocationService;
        #endregion
    
        public SKUAllocationModelFactory(VendorSettings vendorSettings,
                                        IWorkContext workContext,
                                        SKUAllocationService skuAllocationService)
        {

            _workContext= workContext;
            _vendorSettings= vendorSettings;
            _skuAllocationService = skuAllocationService;
        }
        #endregion
<more methods snipped>


I have tried registering my objects in my nop startup:

// Create the container builder.
            var builder = new ContainerBuilder();
            builder.RegisterType <IRepository<SkuAllocation>>().As<IRepository<SkuAllocation>>();
            builder.RegisterType<ILogger>().As<ILogger>();
            builder.RegisterType<SKUAllocationService>();
            
            var container = builder.Build();

            using (var scope = container.BeginLifetimeScope())
            {
                var component = scope.Resolve<SKUAllocationService>();
            }

this just gets a a similar constructor not found error  

Can anyone point me to where I'm going wrong
1 ano atrás
*moved to general
1 ano atrás
maltadonna wrote:
I have tried registering my objects in my nop startup:

// Create the container builder.
            var builder = new ContainerBuilder();
            builder.RegisterType <IRepository<SkuAllocation>>().As<IRepository<SkuAllocation>>();
            builder.RegisterType<ILogger>().As<ILogger>();
            builder.RegisterType<SKUAllocationService>();    

This is the old way

What does the Controller Ctor look like - what is missing and not registered ?
1 ano atrás
Can you link to the new way? I'm also running into issues refactoring my plugin to not use the following code in an \Infrastructure\DependencyRegistrar.cs file

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


My general error is "None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder... etc

I have attempted to create a NopStartup class with the following but I still get the autofac errors..

public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            services.AddSingleton<ISyncService, SyncService>();
        }
1 ano atrás
FWIW I had to build again.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.