.blazor dependency injection error: An unhandled exception occurred while processing the request.?

Viewed 541

Using SqlSugar ORM, based on blazor, dependency injection business service, an error is reported when calling。 SqlSugarService:

 public static class SqlSugarService
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(SqlSugarService));
        public static void AddSqlSugarSevice(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));
        
            services.AddScoped<ISqlSugarClient>(o =>
            {
            
                var listConfig = new List<ConnectionConfig>();
                listConfig.Add(new ConnectionConfig
                {
                    DbType = DbType.SqlServer,
                    ConnectionString = "Server=.\\SQLEXPRESS;DataBase=Test;Uid=sa;Pwd=123456",
                    IsAutoCloseConnection = true,
                    InitKeyType = InitKeyType.Attribute
                });
                var dbContext = new SqlSugarClient(listConfig);
                
                return dbContext;
               
            });

        }

    }

the interface:

  public interface IReportRepository
    {
          public DataTable GetTest(string sql);
    }

Interface implementation:

 public class ReportRepository : IReportRepository
    {
        private ISqlSugarClient _dbBase;
        public ReportRepository(ISqlSugarClient sqlSugar)
        {
             _dbBase = sqlSugar;
         }
    
        
        public DataTable GetTest(string sql)
        {
          return _dbBase.Ado.GetDataTable(sql);
        }

}

injection:

  services.AddSqlSugarSevice();
  services.TryAddTransient<IReportRepository, ReportRepository>(); 

use constructor injection:

   private readonly IReportRepository _reportRepository;

        //[Inject]
        //public IReportRepository ReportService { get; set; }

        public Report(IReportRepository  reportRepository)
        {
            _reportRepository = reportRepository;
            _reportRepository.GetTest("select * from test");
        }

ERROR :

An unhandled exception occurred while processing the request. MissingMethodException: No parameterless constructor defined for type 'MyReport.Pages.Report'. System.RuntimeType.CreateInstanceDefaultCtorSlow(bool publicOnly, bool wrapExceptions, bool fillCache)

enter image description here

public partial class Report
{
    private readonly IReportRepository _reportRepository;

    //[Inject]
    //public IReportRepository ReportService { get; set; }
    public Report()
    {
        
    }
    public Report(IReportRepository  reportRepository)
    {
        _reportRepository = reportRepository;
        _reportRepository.GetTest("select * from test");
    }


}

enter image description here

1 Answers

In a Razor Page (Component) you have to use Property Injection, not Constructor Injection. And that means that the injected services are not available in the constructor. That's why we have OnInitialized().

I fixed what I could see, remove the commented-out parts:

public partial class Report
{
    //private readonly IReportRepository _reportRepository;

    [Inject]
    public IReportRepository ReportService { get; set; }
    
    //public Report()
    //{        
    //}
    //public Report(IReportRepository  reportRepository)
    //{
    //    _reportRepository = reportRepository;
    //    _reportRepository.GetTest("select * from test");
    //}

    protected override OnItializedAsync()
    {
       // replace with async version if possible
       // and assign to something?
       ReportService.GetTest("select * from test");                 
    }

}
Related