Can';似乎无法在finally块中刷新Blazor页面
无论是否遇到异常,我都会尝试刷新页面。使用下面的代码,如果用户单击当前被另一个用户锁定的消息,它将弹出一个错误,但它不会运行代码并在捕获错误后刷新my finally块中的页面Can';似乎无法在finally块中刷新Blazor页面,blazor,azureservicebus,Blazor,Azureservicebus,无论是否遇到异常,我都会尝试刷新页面。使用下面的代码,如果用户单击当前被另一个用户锁定的消息,它将弹出一个错误,但它不会运行代码并在捕获错误后刷新my finally块中的页面 async Task GetPayment(GridRowClickEventArgs args) { try { var item = args.Item as NewPayment; sessionId = item.Sessio
async Task GetPayment(GridRowClickEventArgs args)
{
try
{
var item = args.Item as NewPayment;
sessionId = item.SessionId;
selectedPayment = new NewPayment();
receiver = await sbService.CreateReceiver(queue, sessionId);
receivedMessage = await receiver.PeekMessageAsync();
var message = Encoding.UTF8.GetString(receivedMessage.Body);
selectedPayment = JsonSerializer.Deserialize<NewPayment>(message);
paymentWindow = true;
}
catch (Exception ex)
{
toastType = MatToastType.Danger;
Toaster.Add(ex.ToString(), toastType, "", "", configure =>
{
configure.VisibleStateDuration = 100000;
configure.ShowCloseButton = true;
});
}
finally
{
await GetListOfPayments();
StateHasChanged();
}
}
异步任务GetPayment(GridRowClickEventArgs args args)
{
尝试
{
var项目=参数项目作为新付款;
sessionId=item.sessionId;
selectedPayment=新建付款();
receiver=wait sbService.CreateReceiver(队列,会话ID);
receivedMessage=wait receiver.peek消息异步();
var message=Encoding.UTF8.GetString(receivedMessage.Body);
selectedPayment=JsonSerializer.Deserialize(消息);
paymentWindow=true;
}
捕获(例外情况除外)
{
toastType=MatToastType.危险;
添加(例如ToString(),toastType,,,,configure=>
{
configure.VisibleStateDuration=100000;
configure.ShowCloseButton=true;
});
}
最后
{
等待GetListOfPayments();
StateHasChanged();
}
}
GetListOfPayments()代码:
异步任务GetListOfPayments()
{
尝试
{
newPayments=新列表();
newPayments=wait sbService.ListPaymentsInQueue(队列);
}
捕获(例外情况除外)
{
toastType=MatToastType.危险;
添加(例如ToString(),toastType,,,,configure=>
{
configure.VisibleStateDuration=100000;
configure.ShowCloseButton=true;
});
}
}
在接球过程中,烤面包机弹出我没有问题。我尝试将GetListOfPayments()移动到catch,但它不起作用。我让它工作的唯一方法是:
async Task GetPayment(GridRowClickEventArgs args)
{
try
{
var item = args.Item as NewPayment;
sessionId = item.SessionId;
selectedPayment = new NewPayment();
**newPayments = null;**
receiver = await sbService.CreateReceiver(queue, sessionId);
receivedMessage = await receiver.PeekMessageAsync();
var message = Encoding.UTF8.GetString(receivedMessage.Body);
selectedPayment = JsonSerializer.Deserialize<NewPayment>(message);
paymentWindow = true;
}
catch (Exception ex)
{
toastType = MatToastType.Danger;
Toaster.Add(ex.ToString(), toastType, "", "", configure =>
{
configure.VisibleStateDuration = 100000;
configure.ShowCloseButton = true;
});
}
finally
{
**if (newPayments == null)
{
await GetListOfPayments();
StateHasChanged();
}**
}
}
异步任务GetPayment(GridRowClickEventArgs args args)
{
尝试
{
var项目=参数项目作为新付款;
sessionId=item.sessionId;
selectedPayment=新建付款();
**newPayments=null**
receiver=wait sbService.CreateReceiver(队列,会话ID);
receivedMessage=wait receiver.peek消息异步();
var message=Encoding.UTF8.GetString(receivedMessage.Body);
selectedPayment=JsonSerializer.Deserialize(消息);
paymentWindow=true;
}
捕获(例外情况除外)
{
toastType=MatToastType.危险;
添加(例如ToString(),toastType,,,,configure=>
{
configure.VisibleStateDuration=100000;
configure.ShowCloseButton=true;
});
}
最后
{
**如果(newPayments==null)
{
等待GetListOfPayments();
StateHasChanged();
}**
}
}
这会在运行代码获取所选付款之前重置我的数据网格,但会使用更新的付款列表刷新数据网格。理想情况下,我只希望在出现错误时运行GetListOfPayments(),或者至少等到获取所选付款后再运行。可能吗
我还使用Azure ServiceBus创建了一个等待付款的实时队列。我不是100%确定这是正确的方法。欢迎任何意见 哪个数据网格?验证使用
@newPayments?会发生什么情况。计数
而不将其设置为空。我使用的是Telerik Grid,且newPayments上的计数=3。您的网格是否绑定到列表或可观察集合?使用Telerik网格,您最好绑定到ObservableCollection,以确保UI按预期刷新。我更新了代码,使用ObservableCollection而不是列表,但仍然看到相同的结果。paymentCollection=新的ObservableCollection();paymentCollection=等待sbService.ListPaymentsInQueue(队列);尽管如此,还是要展示细节——这更多的是关于网格,而不是Blazor+finally。
async Task GetPayment(GridRowClickEventArgs args)
{
try
{
var item = args.Item as NewPayment;
sessionId = item.SessionId;
selectedPayment = new NewPayment();
**newPayments = null;**
receiver = await sbService.CreateReceiver(queue, sessionId);
receivedMessage = await receiver.PeekMessageAsync();
var message = Encoding.UTF8.GetString(receivedMessage.Body);
selectedPayment = JsonSerializer.Deserialize<NewPayment>(message);
paymentWindow = true;
}
catch (Exception ex)
{
toastType = MatToastType.Danger;
Toaster.Add(ex.ToString(), toastType, "", "", configure =>
{
configure.VisibleStateDuration = 100000;
configure.ShowCloseButton = true;
});
}
finally
{
**if (newPayments == null)
{
await GetListOfPayments();
StateHasChanged();
}**
}
}