可以在自定义 ActionResult 中使用 void async 方法吗?
Can void async method be used in custom ActionResult?
我试图在我的应用程序中封装一些功能,例如,而不是在每个 POST 操作方法中编写这些代码:
var baseUrl = context.HttpContext.Request.Url.Scheme + "://" + context.HttpContext.Request.Url.Authority +
context.HttpContext.Request.ApplicationPath.TrimEnd('/') + "/signalr";
var hubConnection = new HubConnection(baseUrl);
var notification = hubConnection.CreateHubProxy(hubName: HubName);
await hubConnection.Start();
await notification.Invoke(MethodName);
return RedirectToAction("TicketList", "Ticket")
我使用扩展方法和自定义操作结果做了类似的事情:
return RedirectToAction("TicketList", "Ticket").WithSendNotification("notificationHub", "sendNotification");
为了做到这一点,我创建了一个自定义操作结果,并将逻辑放在 ExecuteResult
方法中:
public async override void ExecuteResult(ControllerContext context)
{
var baseUrl = context.HttpContext.Request.Url.Scheme + "://" + context.HttpContext.Request.Url.Authority +
context.HttpContext.Request.ApplicationPath.TrimEnd('/') + "/signalr";
var hubConnection = new HubConnection(baseUrl);
var notification = hubConnection.CreateHubProxy(hubName: HubName);
await hubConnection.Start();
await notification.Invoke(MethodName);
InnerResult.ExecuteResult(context);
}
但是我得到以下错误:
An asynchronous operation cannot be started at this time. Asynchronous
operations may only be started within an asynchronous handler or
module or during certain events in the Page lifecycle. If this
exception occurred while executing a Page, ensure that the Page is
marked <%@ Page Async="true" %>. This exception may also indicate an
attempt to call an "async void" method, which is generally unsupported
within ASP.NET request processing. Instead, the asynchronous method
should return a Task, and the caller should await it.
现在我的问题是 void async
方法可以用于自定义操作结果吗?
更新: ASP.NET 5有这个能力,意味着行动结果除了ActionResult.ExecuteResult
之外还有ActionResult.ExecuteResultAsync。现在我想知道我们如何在 MVC 5.0 中实现这种能力?
How can we implement this ability in MVC 5.0?
你不能。
ASP.NET 如您所见,vNext 是在考虑 async
的情况下从头开始重写的。 ASP.NET(特别是 MVC)的当前版本有一些粗糙的边缘,根本不可能使用 async
。
作为Stephen said, I can't use async
ability inside ExecuteResult
in MVC 5.0. Since my goal was a little bit refactoring I had to use ContinueWith:
public override void ExecuteResult(ControllerContext context)
{
//....
hubConnection.Start().ContinueWith(task =>
{
if (task.IsCompleted)
{
notification.Invoke(MethodName);
}
});
InnerResult.ExecuteResult(context);
}
现在它就像一个魅力。
我试图在我的应用程序中封装一些功能,例如,而不是在每个 POST 操作方法中编写这些代码:
var baseUrl = context.HttpContext.Request.Url.Scheme + "://" + context.HttpContext.Request.Url.Authority +
context.HttpContext.Request.ApplicationPath.TrimEnd('/') + "/signalr";
var hubConnection = new HubConnection(baseUrl);
var notification = hubConnection.CreateHubProxy(hubName: HubName);
await hubConnection.Start();
await notification.Invoke(MethodName);
return RedirectToAction("TicketList", "Ticket")
我使用扩展方法和自定义操作结果做了类似的事情:
return RedirectToAction("TicketList", "Ticket").WithSendNotification("notificationHub", "sendNotification");
为了做到这一点,我创建了一个自定义操作结果,并将逻辑放在 ExecuteResult
方法中:
public async override void ExecuteResult(ControllerContext context)
{
var baseUrl = context.HttpContext.Request.Url.Scheme + "://" + context.HttpContext.Request.Url.Authority +
context.HttpContext.Request.ApplicationPath.TrimEnd('/') + "/signalr";
var hubConnection = new HubConnection(baseUrl);
var notification = hubConnection.CreateHubProxy(hubName: HubName);
await hubConnection.Start();
await notification.Invoke(MethodName);
InnerResult.ExecuteResult(context);
}
但是我得到以下错误:
An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it.
现在我的问题是 void async
方法可以用于自定义操作结果吗?
更新: ASP.NET 5有这个能力,意味着行动结果除了ActionResult.ExecuteResult
之外还有ActionResult.ExecuteResultAsync。现在我想知道我们如何在 MVC 5.0 中实现这种能力?
How can we implement this ability in MVC 5.0?
你不能。
ASP.NET 如您所见,vNext 是在考虑 async
的情况下从头开始重写的。 ASP.NET(特别是 MVC)的当前版本有一些粗糙的边缘,根本不可能使用 async
。
作为Stephen said, I can't use async
ability inside ExecuteResult
in MVC 5.0. Since my goal was a little bit refactoring I had to use ContinueWith:
public override void ExecuteResult(ControllerContext context)
{
//....
hubConnection.Start().ContinueWith(task =>
{
if (task.IsCompleted)
{
notification.Invoke(MethodName);
}
});
InnerResult.ExecuteResult(context);
}
现在它就像一个魅力。