Net Core 3.1 返回一个任务,其结果包含异常的详细信息?



我正在使用 ADO.Net 来填充数据表,这是我放入单独存储库的数据表填充方法。我将存储库/接口注入到测试页面模型中。页面模型方法"OnPostUpdateAlarmDataTableAsync"需要调用接口并请求要填充的数据表。我想从填充此表的任务返回结果,因此如果任务由于某种原因失败,我可以在 UI 上通知用户,我还想从页面模型中记录错误的详细信息,因为这是调用线程。

当前的编译错误说"无法将 void 分配给隐式类型变量"但是无论我尝试的代码更改中的什么组合,都无法正确编译。能够从异步操作中返回任务,其中包含有关异常的详细信息的相关信息是我想要实现的目标。

接口:

public interface IAdoNetRepository
{
Task FillAlarmsDataTable();
}

存储 库:

public class AdoNetRepository : IAdoNetRepository
{
public readonly IConfiguration _config;
public AdoNetRepository(IConfiguration config)
{
_config = config;
}
// Used for method below GetUserGroupsData()
public static SqlDataAdapter dataAdapter = new SqlDataAdapter();
public static DataTable alarmDataTable;

public async Task FillAlarmsDataTable()
{
string connectionString = _config.GetConnectionString("Data:DefaultConnection:ConnectionString");
try
{
SqlConnection connection = new SqlConnection(connectionString);
string cmdText1 = "SELECT * FROM [dbo].[Alarms] ORDER BY Name";
// Create a new data adapter based on the specified query.
dataAdapter = new SqlDataAdapter(cmdText1, connection);
// Populate a new data table and bind it to the BindingSource.
alarmDataTable = new DataTable
{
Locale = CultureInfo.InvariantCulture
};
await Task.Run(() => dataAdapter.Fill(alarmDataTable));
//dataAdapter.Fill(alarmDataTable);
return; // Return what ?
}
catch (Exception ex)
{
// Return the task with details of the exception
return; // Return what?
}
}
}

页面型号:

public class TestModel : PageModel
{
private readonly IAdoNetRepository _adoNetRepository;
public TestModel(IAdoNetRepository adoNetRepository)
{
_adoNetRepository = adoNetRepository;
}
public async Task<IActionResult> OnPostUpdateAlarmDataTableAsync()
{
// This gets squiggly line compile error            
var result = await _adoNetRepository.FillAlarmsDataTable();
if (!result.Succeeded)
{
// log the exception returned from the task that failed!
}
return new JsonResult(result);
}
}

原始示例中的注释显示了您实际应该执行的操作。

您需要创建一个模型来存储所需的信息。

类似的东西

/// <summary>
/// Represents the result of an ADO.Net operation.
/// </summary>
public class AdoNetResult {
private List<Exception> _errors = new List<Exception>();
public bool Succeeded { get; protected set; }
public IEnumerable<Exception> Errors => _errors;
public static AdoNetResult Success { get; } = new AdoNetResult { Succeeded = true };
public static AdoNetResult Failed(params Exception[] errors) {
var result = new AdoNetResult { Succeeded = false };
if (errors != null) {
result._errors.AddRange(errors);
}
return result;
}
}

但是,接口需要重构

public interface IAdoNetRepository {
Task<AdoNetResult> FillAlarmsDataTable();
}

和实施

public async Task<AdoNetResult> FillAlarmsDataTable() {
string connectionString = _config.GetConnectionString("Data:DefaultConnection:ConnectionString");
try {
SqlConnection connection = new SqlConnection(connectionString);
string cmdText1 = "SELECT * FROM [dbo].[Alarms] ORDER BY Name";
// Create a new data adapter based on the specified query.
dataAdapter = new SqlDataAdapter(cmdText1, connection);
// Populate a new data table and bind it to the BindingSource.
alarmDataTable = new DataTable {
Locale = CultureInfo.InvariantCulture
};
await Task.Run(() => dataAdapter.Fill(alarmDataTable));
// Return what ?
return AdoNetResult.Success; 
} catch (Exception ex) {
// Return the task with details of the exception
return AdoNetResult.Failed(ex);
}
}

完成此操作后,原始代码应按预期工作

public async Task<IActionResult> OnPostUpdateAlarmDataTableAsync() {
var result = await _adoNetRepository.FillAlarmsDataTable();
if (!result.Succeeded) {
// log the exception returned from the task that failed!
//result.Errors
}
return new JsonResult(result);
}

另一种方法是让FillAlarmsDataTable抛出它可能遇到的任何异常

public Task FillAlarmsDataTable() {
string connectionString = _config.GetConnectionString("Data:DefaultConnection:ConnectionString");
SqlConnection connection = new SqlConnection(connectionString);
string cmdText1 = "SELECT * FROM [dbo].[Alarms] ORDER BY Name";
// Create a new data adapter based on the specified query.
dataAdapter = new SqlDataAdapter(cmdText1, connection);
// Populate a new data table and bind it to the BindingSource.
alarmDataTable = new DataTable {
Locale = CultureInfo.InvariantCulture
};
return Task.Run(() => dataAdapter.Fill(alarmDataTable));
}

捕获(捕获(正在使用异常的异常并相应地响应

public async Task<IActionResult> OnPostUpdateAlarmDataTableAsync() {
try {
await _adoNetRepository.FillAlarmsDataTable();
return Ok(); //200 OK
catch(Exception ex) {
//...log the exception returned from the task that failed!
return new ExceptionResult(ex, includeErrorDetail:true);
}
}

最新更新