ASP.NET Core blazor dependency injection error: System.NullReferenceException?

2

Using SqlSugar ORM, based on blazor, dependency injection business service, an error is reported when calling, and it is empty。 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>(); 

Used in component code:

public partial class Report
{
    [Inject]
    public IReportRepository ReportService { get; set; }

    public Report()
    {
        ReportService.GetTest("select * from test");
    }
}

ERROR :

System.NullReferenceException,HResult=0x80004003,Message=Object reference not set to an instance of an object, Source=MyReport

Screenshot of error message

c#
.net
asp.net-core
orm
blazor
asked on Stack Overflow May 11, 2021 by Wihelm jie • edited May 11, 2021 by dbc

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0