控制器从服务中无法获取ILogger和SugDbContext的时候排除异常

This commit is contained in:
falcon 2022-03-22 15:30:20 +08:00
parent 8a8160bf9e
commit 41c41878e1

View File

@ -39,11 +39,14 @@ namespace Falcon.SugarApi.ApiDefinistions
return $":{con}:{ac}";
}
}
/// <summary>
/// 构造控制器基类
/// </summary>
/// <param name="service"></param>
protected ApiControllerBase(IServiceProvider service) {
this.Services = service;
this.Logger = service.GetService(typeof(ILogger<>).MakeGenericType(GetType())) as ILogger;
this.SugarDb = service.GetService<SugarDbContext>();
this.Logger = service.GetService(typeof(ILogger<>).MakeGenericType(GetType())) as ILogger ?? throw new NullReferenceException("ILogger");
this.SugarDb = service.GetService<SugarDbContext>() ?? throw new NullReferenceException("SugarDbContext");
}
/// <summary>
@ -77,7 +80,7 @@ namespace Falcon.SugarApi.ApiDefinistions
/// <typeparam name="T">对象的类型</typeparam>
/// <param name="json">json字符串</param>
/// <returns>对象实例</returns>
protected T JsonDeserialize<T>(string json) {
protected T? JsonDeserialize<T>(string json) where T: class {
return JsonSerializer.Deserialize<T>(json);
}