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

如何验证当前Cosmos Client连接的区域?V3版本替代方案

获取CosmosClient的写入与读取端点

在Azure Cosmos DB的.NET SDK V3版本中,DocumentClient已被标记为过时,取而代之的是CosmosClient。要获取对应的写入和读取端点,可按以下方式操作:

  • 写入端点:直接通过CosmosClient.Endpoint属性获取,该属性默认返回当前账户的写入端点。
  • 读取端点:若配置了多区域读取偏好,可通过CosmosClient.ClientOptions.ApplicationPreferredRegions获取首选读取区域,再结合账户名称构造读取端点;若未配置特定读取偏好,SDK默认会使用写入端点作为读取端点。

代码示例:

// 初始化CosmosClient
var cosmosClient = new CosmosClient("your-connection-string");

// 获取写入端点
Uri writeEndpoint = cosmosClient.Endpoint;

// 获取首选读取区域对应的读取端点
var preferredReadRegion = cosmosClient.ClientOptions.ApplicationPreferredRegions?.FirstOrDefault();
Uri? readEndpoint = null;
if (!string.IsNullOrEmpty(preferredReadRegion))
{
    // 构造读取端点URL
    var accountName = cosmosClient.Endpoint.Host.Split('.')[0];
    readEndpoint = new Uri($"https://{accountName}-{preferredReadRegion}.documents.azure.com:443/");
}

提示:如果需要从操作响应中确认实际使用的读取端点,也可以在执行查询或读取操作后,从响应的x-ms-current-region头中获取当前使用的区域,再构造对应的端点。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 14:49:58