Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .blazor依赖项注入错误:处理请求时发生未处理的异常。?_C#_.net_Asp.net Core_Orm_Blazor - Fatal编程技术网

C# .blazor依赖项注入错误:处理请求时发生未处理的异常。?

C# .blazor依赖项注入错误:处理请求时发生未处理的异常。?,c#,.net,asp.net-core,orm,blazor,C#,.net,Asp.net Core,Orm,Blazor,使用基于blazor(依赖项注入业务服务)的SqlSugar ORM,调用时会报告一个错误 SqlSugarService: public static class SqlSugarService { private static readonly ILog log = LogManager.GetLogger(typeof(SqlSugarService)); public static void AddSqlSugarSevice(this ISer

使用基于blazor(依赖项注入业务服务)的SqlSugar ORM,调用时会报告一个错误 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;
               
            });

        }

    }
接口实现:

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

}
注射:

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

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

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

处理请求时发生未处理的异常。 MissingMethodException:没有为类型“MyReport.Pages.Report”定义无参数构造函数。 System.RuntimeType.CreateInstanceDefaultCtorSlow(bool publicOnly、bool WrapeExceptions、bool fillCache)

在Razor页面(组件)中,必须使用属性注入,而不是构造函数注入。这意味着注入的服务在构造函数中不可用。这就是为什么我们要初始化()

我修复了我能看到的,删除了注释掉的部分:

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");                 
    }

}

“没有为类型'MyReport.Pages.Report'定义无参数构造函数”-向我们展示该类。明白了,谢谢添加无参数构造函数后,没有报告错误,但我不知道为什么,添加了类代码。注入方法错误?我想注入,只需使用您应该再次删除它b-您现在拥有
\u reportRepository==null
谢谢,我现在明白了
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");
    }


}
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");                 
    }

}