None of the constructors found with 'Autofac.Core.Activators .Reflection.DefaultConstructorFinder' on type

3 个月 前
I m developing a plugin with data base acess and AJAX Request

I registered my service using an interface that is located in my own plugin project.

Controller
namespace Nop.Plugin.Widgets.Samurai.Controller
{
    [Authorize]
    public class SamuraiDataBaseMigrationController : BasePluginController
    {
        private readonly SamuraiDataService _service;

        public SamuraiDataBaseMigrationController(SamuraiDataService service)
        {
            _service = service;
        }

        [HttpPost]
        public ActionResult AdicionarAoMigrate([FromBody] ProductInfoModel data)
        {
            _service.ArmazenarDados(data.ProductName, data.ProductPrice);
            return Json(new { success = true, message = "Informações do produto migradas com sucesso!" });
        }


    }

}


Migration

 public class SamuraiMigration : AutoReversingMigration
{
  
     public override void Up()
     {
         if (!DataSettingsManager.IsDatabaseInstalled())
             return;

        //Cria a tabela com o nome ProductInfo e adiciona duas colunas, uma sendo do nome do produto e outra para preços

         Create.Table("ProductsInfo")
        .WithColumn("ProductName").AsString(256).NotNullable()
        .WithColumn("ProductPrice").AsDecimal().NotNullable();  
      
     }

    

}


Service

namespace Nop.Plugin.Widgets.Samurai.Services
{
    public interface ISamuraiDataService
    {
        void ArmazenarDados(string productname, float productprice);
    }

    public class SamuraiDataService : ISamuraiDataService
    {
        private readonly IDataContext _dataContext;

        public SamuraiDataService(IDataContext dataContext)
        {
            _dataContext = dataContext;
        }

        public void ArmazenarDados(string productname, float productprice)
        {
            var newRegister = new ProductInfoModel
            {
                ProductName = productname,
                ProductPrice = productprice
            };

            _dataContext.Insert(newRegister);
        }

    }
}

NopServices

namespace Nop.Plugin.Widgets.Samurai.Infrastructure
{
    public class NopStartup : INopStartup
    {
        public int Order => 2000;
        public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            services.AddScoped<SamuraiMigration>();
            services.AddScoped<SamuraiDataBaseMigrationController>();
            services.AddScoped<SamuraiDataService>();
        }
        public void Configure(IApplicationBuilder application)
        {
            //nothing
        }
    }
}

Ajax Request

  <div class="add-to-migrate-button">
    <button class="add-to-migrate" id="add-to-migrate" type="button">ADD TO BASE</button>
  </div>

  <script>
    $(document).ready(function () {
      $("#add-to-migrate").click(function () {
        var ProductName = $(".product-name").text();
        var ProductPrice = parseFloat($(".product-price").text().replace(/[^\d.-]/g, ''));
        console.log("URL:", '@Url.RouteUrl("AdicionarAoMigrate")');
        


        armazenarDadosNoBanco(ProductName, ProductPrice);
      });

      function armazenarDadosNoBanco(ProductName, ProductPrice) {
        $.ajax({
          url: '@Url.RouteUrl("AdicionarAoMigrate")',
          cache: false,
          type: "POST",
          dataType: "json",
          contentType: "application/json",
          data: JSON.stringify({
            ProductName: ProductName,
            ProductPrice: ProductPrice
          }),
          success: function (data) {
            console.info("Successful data migration")
          },
          error: function (xhr, status, error) {
            console.error("AJAX request error:", error);
            console.error("Status:", status);
            console.error("Response:", xhr.responseText);
          }
          
        });
      }
    });
  </script>
}


3 个月 前
JoaoBahia wrote:
public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            services.AddScoped<SamuraiDataService>();


What version of nopCommerce ?
Typically the form of AddScoped code in the Startup is

            services.AddScoped<ISamuraiDataService, SamuraiDataService>();
3 个月 前
Hello, my friend. Thanks for your attencion, the version of nopCommerce is 4.60.5. After adding your recommendation, the error persists: Autofac.Core.DependencyResolutionException: An exception was thrown while activating Nop.Plugin.Widgets.Samurai.Controller.SamuraiDataBaseMigrationController.
---> Autofac.Core.DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Nop.Plugin.Widgets.Samurai.Controller.SamuraiDataBaseMigrationController' can be invoked with the available services and parameters:
Cannot resolve parameter 'Nop.Plugin.Widgets.Samurai.Services.SamuraiDataService service' of constructor 'Void .ctor(Nop.Plugin.Widgets.Samurai.Services.SamuraiDataService)'.
3 个月 前
I believe that you should check nopCommerce plugins that have used Migration to better understand code flow or check this page on their documentation website because this one has a clear view to the solution of your problem.

Best regards,
Atul
3 个月 前
Thank you for your attention!
3 个月 前
you can check
Nop.Plugin.Shipping.FixedByWeightByTotal plugin Migrations folder
there you will find the implementation of AutoReversingMigration & Migration class.
//Rashed
3 个月 前
I've now updated the migration class, but the error still persists, I've also updated the dependency injection code


namespace Nop.Plugin.Widgets.Samurai.DataBase
{
    [NopMigration("2024-01-23 17:00:00", "Widgets.Samurai base schema", MigrationProcessType.Installation)]
    public class SamuraiMigration : AutoReversingMigration
    {
      
        public override void Up()
        {
            Create.TableFor<ProductInfoModel>();
          
        }

        

    }
}



 public class NopStartup : INopStartup
{
     public int Order => 3000;
     public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
     {
        
         services.AddScoped<ISamuraiDataService, SamuraiDataService>();
     }
     public void Configure(IApplicationBuilder application)
     {
         //nothing
     }
}
3 个月 前
Can you share a code example of this SamuraiDataBaseMigrationController. what do you want to do here?
//Rashed
3 个月 前
The intention of this plugin is to receive the name and price of a product from the products view of my nopcommerce store, through an ajax request to my controller, and within the controller it migrates the name and price to a table in the database, but when I go to make the request this autofac error occurs.

public class SamuraiDataBaseMigrationController : BasePluginController
{
    private readonly ISamuraiDataService _service;

    public SamuraiDataBaseMigrationController(ISamuraiDataService service)
    {
        _service = service;
    }

    [HttpPost]
    public ActionResult AdicionarAoMigrate([FromBody] ProductInfoModel data)
    {
        _service.ArmazenarDados(data.ProductName, data.ProductPrice);
        return Json(new { success = true, message = "Informações do produto migradas com sucesso!" });
    }


}
3 个月 前
nopStation wrote:
Can you share a code example of this SamuraiDataBaseMigrationController. what do you want to do here?
//Rashed


The intention of this plugin is to receive the name and price of a product from the products view of my nopcommerce store, through an ajax request to my controller, and within the controller it migrates the name and price to a table in the database, but when I go to make the request this autofac error occurs.

public class SamuraiDataBaseMigrationController : BasePluginController
{
    private readonly ISamuraiDataService _service;

    public SamuraiDataBaseMigrationController(ISamuraiDataService service)
    {
        _service = service;
    }

    [HttpPost]
    public ActionResult AdicionarAoMigrate([FromBody] ProductInfoModel data)
    {
        _service.ArmazenarDados(data.ProductName, data.ProductPrice);
        return Json(new { success = true, message = "Informações do produto migradas com sucesso!" });
    }


}