自己包装器中的 Azure ServiceBus TopicClient SendAsync 实现

Azure ServiceBus TopicClient SendAsync implementation in own wrapper

Azure ServiceBus TopicClient 的 SendAsync 方法的正确实现是什么?

在第二个实现中,BrokeredMessage 实际上会在 SendAsync 发生之前被释放吗?

    public async Task SendAsync<TMessage>(TMessage message, IDictionary<string, object> properties = null)
    {
        using (var bm = MessagingHelper.CreateBrokeredMessage(message, properties))
        {
            await this._topicClient.Value.SendAsync(bm);
        }
    }


    public Task SendAsync<TMessage>(TMessage message, IDictionary<string, object> properties = null)
    {
        using (var bm = MessagingHelper.CreateBrokeredMessage(message, properties))
        {
            return this._topicClient.Value.SendAsync(bm);
        }
    }

我想从 await/async 模式中获得最大收益。

回答你的问题:第二种方法可能会导致处置对象出现问题,你必须等待 SendAsync 执行结束才能释放资源。

详细解释。

如果你调用await,一个方法的执行将同时停止,直到没有返回可等待的方法才会继续。代理消息将存储在本地隐藏变量中,不会被释放。

如果您不调用 await,执行将继续并且代理消息的所有资源将在实际使用之前被释放(因为 using 在对象上调用 Dispose最后)或在消费过程中。这肯定会导致 SendAsync 内的异常。至此,SendAsync的执行才真正开始.

await 所做的是“暂停”任何当前线程并等待任务完成及其结果。而这正是您真正需要的。 async-await 的目的是允许某些任务与其他任务同时执行,它提供了在确实需要时等待并发操作结果的能力,并且没有它就不可能进一步执行。

如果顶部的每个方法也是异步方法,则第一种方法很好。我的意思是,如果您的 SendAsync 的调用者是异步任务,并且该调用者的调用者等等到顶级调用方法。

此外,考虑可能引发的异常,它们已列出 here. As you can see, there are so-called transient errors. This is a kind of errors that retry can possibly fix. In your code, there is no such exception handling. Example of retry pattern could be found here,但提到的关于异常的文章可以提出更好的解决方案,这是另一个问题的主题。我还会添加一些日志系统以至少了解任何非瞬态异常。