我目前正在尝试创建一个可用于ASP.NET Web API的,它将允许结果为异步>。目前,我忽略了转换,只关心和类型的值。我目前有以下实现:
type AsyncApiActionInvoker() =
inherit Controllers.ApiControllerActionInvoker()
override x.InvokeActionAsync(actionContext, cancellationToken) =
if actionContext = null then
raise <| ArgumentNullException("actionContext")
let actionDescriptor = actionContext.ActionDescriptor
Contract.Assert(actionDescriptor <> null)
if actionDescriptor.ReturnType = typeof<Async<HttpResponseMessage>> then
let controllerContext = actionContext.ControllerContext
Contract.Assert(controllerContext <> null)
let task = async {
let! asyncResult = Async.AwaitTask <| actionDescriptor.ExecuteAsync(controllerContext, actionContext.ActionArguments, cancellationToken)
// For now, throw if the result is an IHttpActionResult.
if typeof<IHttpActionResult>.IsAssignableFrom(actionDescriptor.ReturnType) then
raise <| InvalidOperationException("IHttpResult is not supported when returning an Async")
let! result = asyncResult :?> Async<HttpResponseMessage>
return actionDescriptor.ResultConverter.Convert(controllerContext, result) }
Async.StartAsTask(task, cancellationToken = cancellationToken)
else base.InvokeActionAsync(actionContext, cancellationToken)
这仅适用于Async<HttpResponseMessage>
。 如果我尝试强制转换为Async<_>
,则会出现异常,指出无法转换为Async<obj>
。 我也无法正确检测actionDescriptor.ReturnType
是否是Async<_>
。 这并不让我感到惊讶,但我不知道该如何解决问题。