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

使用ClientSecretCredential连接Azure Service Bus认证失败求助

问题:Azure Service Bus连接失败,Rider中报错Response was not set, make sure SendAsync was called

报错详情

Azure.Identity.AuthenticationFailedException: ClientSecretCredential authentication failed: Response was not set, make sure SendAsync was called

涉事代码

var adminClient = new ServiceBusAdministrationClient(
    mySettings.AzureServiceBus.Endpoint,
    new ClientSecretCredential(mySettings.AzureServiceBus.TenantId,
        mySettings.AzureServiceBus.ClientId, mySettings.AzureServiceBus.ClientSecret));

var exists = await adminClient.SubscriptionExistsAsync(myTopic, mySubName).ConfigureAwait(false); 

环境差异

  • Windows 11 + JetBrains Rider:每次运行均失败,仅偶尔在调试时查看客户端属性后能成功(无稳定规律)
  • Visual Studio(同事环境)/ Visual Studio Community(本地安装后):运行完全正常

诊断线索

AzureEventSourceListener显示,向login.microsoftonline.com发送实例发现请求时抛出System.InvalidOperationException,堆栈信息与上述报错一致。


解决方法

  1. 升级Azure相关NuGet包
    旧版Azure.Identity库存在HttpClient初始化的兼容性问题,会导致部分IDE环境下请求未正确触发。将Azure.Identity和Azure.Messaging.ServiceBus(含管理客户端依赖包)升级至最新稳定版本。

  2. 显式指定HttpClient实例
    手动创建HttpClient并传入ClientSecretCredential,规避默认工厂在Rider环境中的异常行为:

    var httpClient = new HttpClient();
    var credentialOptions = new ClientSecretCredentialOptions
    {
        HttpClient = httpClient
    };
    
    var credential = new ClientSecretCredential(
        mySettings.AzureServiceBus.TenantId,
        mySettings.AzureServiceBus.ClientId,
        mySettings.AzureServiceBus.ClientSecret,
        credentialOptions);
    
    var adminClient = new ServiceBusAdministrationClient(
        mySettings.AzureServiceBus.Endpoint, credential);
    
  3. 检查Rider代理配置
    确认Rider的HTTP代理设置是否与Visual Studio一致,错误的代理会阻断向login.microsoftonline.com的请求。路径:File > Settings > Appearance & Behavior > System Settings > HTTP Proxy,建议设置为Auto-detect proxy settings或匹配Visual Studio的代理配置。

  4. 调整Rider调试设置
    部分调试优化会干扰异步请求执行,尝试禁用Settings > Build, Execution, Deployment > Debugger > Async Stack Traces中的选项,或使用Run without Debugging(Ctrl+F5)测试是否恢复正常。

  5. 清理Rider缓存
    缓存损坏可能导致环境异常,执行File > Invalidate Caches...,选择Invalidate and Restart后重新运行项目。


内容的提问来源于stack exchange,提问作者fp-jcorriveau

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 14:05:22