包含带异步调用的while循环的F#异步函数无法编译

时间:2019-05-29 19:54:25

标签: f#

不确定我的代码为何无法编译,这是我遇到的错误: Incomplete structured construct at or before this point in expression

let resendErrorsAsync (bus: IBus) (errorQueueName: string) =
async {
    let! errorQueue = bus.Advanced.QueueDeclareAsync(errorQueueName) |> Async.AwaitTask
    let! message = bus.Advanced.GetMessageAsync(errorQueue) |> Async.AwaitTask
    while message <> null do
        let utf8Body = Encoding.UTF8.GetString(message.Body)
        let error = JsonConvert.DeserializeObject<Error>(utf8Body)
        let errorBodyBytes = Encoding.UTF8.GetBytes(error.Message)
        let! exchange = bus.Advanced.ExchangeDeclareAsync(error.Exchange, "topic") |> Async.AwaitTask
        let! message = bus.Advanced.GetMessageAsync(errorQueue) |> Async.AwaitTask
}

这似乎与我在while循环中的两个异步调用有关,但不确定为什么。

1 个答案:

答案 0 :(得分:4)

您不能以let!结尾的块,它后面必须有一个主体。这就是语法错误的原因。

您在最后一行定义的message与在while条件下测试的变量不同。看来您想改变讯息;为此,您必须显式创建一个可变变量:

let resendErrorsAsync (bus: IBus) (errorQueueName: string) =
    async {
        let! errorQueue = bus.Advanced.QueueDeclareAsync(errorQueueName) |> Async.AwaitTask
        let! msg = bus.Advanced.GetMessageAsync(errorQueue) |> Async.AwaitTask
        let mutable message = msg
        while message <> null do
            let utf8Body = Encoding.UTF8.GetString(message.Body)
            let error = JsonConvert.DeserializeObject<Error>(utf8Body)
            let errorBodyBytes = Encoding.UTF8.GetBytes(error.Message)
            let! exchange = bus.Advanced.ExchangeDeclareAsync(error.Exchange, "topic") |> Async.AwaitTask
            let! msg = bus.Advanced.GetMessageAsync(errorQueue) |> Async.AwaitTask
            message <- msg
    }