You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure ServiceBus SendMessageAsync偶发性能劣化问题排查求助

Azure ServiceBus 随机发送/接收超时排查求助

我有一个部署在Azure上的.NET Web API,已将消息发布者、ServiceBusClient和ServiceBusAdministrationClient都注册为单例服务。近期随机遇到性能问题,定位发现await sender.SendMessageAsync(message)是瓶颈之一:多数时候仅耗时数毫秒,但每10次调用里会有几次耗时超过10秒。

发布消息代码

public async Task PublishMessageAsync<T>(T entity, bool isQueue = false)
{
    var queueOrTopicName = "{my topic name}";

    try
    {
        if (ConnectionIsClosed())
            OpenClient();

        if (_client == null)
            return;

        if (!await _serviceBusAdministrator.MessageNamespaceExists(queueOrTopicName, isQueue))
            throw new Exception($"Unable to create message namespace for {queueOrTopicName}!");

        var serializedBody = JsonSerializer.Serialize(entity);
        var message = new ServiceBusMessage(serializedBody);

        // 复用Sender尝试优化性能
        ServiceBusSender sender = null;
        if (_senders.ContainsKey(queueOrTopicName))
        {
            sender = _senders[queueOrTopicName];
        }
        else
        {
            sender = _client.CreateSender(queueOrTopicName);
            _senders.Add(queueOrTopicName, sender);
        }

        // 此处随机出现超长耗时
        await sender.SendMessageAsync(message).ConfigureAwait(false);
    }
    catch (TimeoutException ex)
    {
        _logger.LogInformation(ex, $"Timeout exception raised when publishing to {queueOrTopicName}. Detail: {ex.Message}");
    }
}

使用的ServiceBus包版本

<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.11.1" />

更奇怪的是,在App Insights中,ServiceBus的Receive调用也出现极长耗时,不确定两者是否相关,但确实属于异常情况。我们使用的是标准层,且消息量并不大,因此排除容量不足的问题,希望有人能提供排查方向,非常感谢。

App Insights中ServiceBus Receive调用耗时异常截图

内容的提问来源于stack exchange,提问作者Brandon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 16:55:20