[英]async/await with signalr hub methods
So i have started on a uber for 'x' app.所以我已经开始为“x”应用程序开发 uber。 Same concept as Uber.
与优步相同的概念。 And the following code is the code on my Signalr hub.
以下代码是我的 Signalr 集线器上的代码。 I use signalr to communicate between client and server.
我使用 signalr 在客户端和服务器之间进行通信。 So when a client request for help it connects to RequestForHelp.
因此,当客户端请求帮助时,它会连接到 RequestForHelp。 Then the RequestForHelp sends back a request to the right supplier.
然后 RequestForHelp 将请求发送回正确的供应商。 And depending on what the supplier answers either HelpResponseYes HelpResponseNo is called.
根据供应商的回答,HelpResponseYes HelpResponseNo 被调用。 I want to make this a async.
我想让它成为异步的。 I want to make the requestforhelp a task.
我想让请求帮助成为一项任务。 So when i click the request button the program awaits.
因此,当我单击请求按钮时,程序会等待。 Any suggestions how you can do that?
有什么建议可以做到吗?
public void RequestForHelp(RequestDetails requestDetails, Location customerClientLocation, int customerClientId)
{
Customer Customer = Service.CustomerService.GetCustomerById(requestDetails.CustomerId);
Customer.ClientId = customerClientId;
Customer.ClientLocation = customerClientLocation;
requestDetails.Customer = Customer;
Clients.User(requestDetails.NearestSupplierList.FirstOrDefault().AspNetUserID).requestForHelpInClient(requestDetails);
}
public void HelpResponseYes(RequestDetails requestDetails)
{
//A supplier is matched
}
public void HelpResponseNo(RequestDetails requestDetails)
{
//next supplier on the list
}
The only reason to use async
is if you have something to await
.使用
async
的唯一原因是你有什么要await
的。 So I'll assume that you will make your requestForHelpInClient
asynchronous and return a Task
, and you can await
that.因此,我假设您将
requestForHelpInClient
异步并返回一个Task
,您可以await
。
It's as easy as this:就这么简单:
public async void RequestForHelp(RequestDetails requestDetails, Location customerClientLocation, int customerClientId)
{
Customer Customer = Service.CustomerService.GetCustomerById(requestDetails.CustomerId);
Customer.ClientId = customerClientId;
Customer.ClientLocation = customerClientLocation;
requestDetails.Customer = Customer;
await Clients.User(requestDetails.NearestSupplierList.FirstOrDefault().AspNetUserID).requestForHelpInClient(requestDetails);
}
However, you always have to think hard when you see async void
, because:但是,当您看到
async void
时,您总是需要认真思考,因为:
try
/ catch
blocks around anything that can throw an exception, andtry
/ catch
块放在任何可能引发异常的地方,并且 I have had trouble myself with using async
event handlers with SignalR because of point #2.由于第 2 点,我在使用带有 SignalR 的
async
事件处理程序时遇到了麻烦。 Because SignalR cannot wait for your async
method to complete, it might start processing another request before the first one is finished .因为 SignalR 无法等待您的
async
方法完成,它可能会在第一个请求完成之前开始处理另一个请求。 That may or may not be a problem for you.这对你来说可能是也可能不是问题。
It was a problem for me, so I kept my event handlers synchronous.这对我来说是个问题,所以我让我的事件处理程序保持同步。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.