Azure Cosmos DB - 如何在不同的控制器中使用不同的容器 ID 实例化 Cosmos Db

Azure Cosmos DB - How to instantiate Cosmos Db with a different container Id in different controllers

我正在使用 .net 6

我正在尝试使用 CosmosDb,并且我正在学习 this 教程。问题是它们仅基于 appsettings.json

中设置的容器 ID 进行实例化

就是这样Program.cs

static async Task<CosmosDbService> InitializeCosmosClientInstanceAsync(IConfigurationSection configurationSection)
{
    var databaseName = configurationSection["DatabaseName"];
    var containerName = configurationSection["ContainerName"];
    var account = configurationSection["Account"];
    var key = configurationSection["Key"];
    var client = new CosmosClient(account, key);
    var database = await client.CreateDatabaseIfNotExistsAsync(databaseName);
    await database.Database.CreateContainerIfNotExistsAsync(containerName, "/id");
    var cosmosDbService = new CosmosDbService(client, databaseName, containerName);
    return cosmosDbService;
}

builder.Services.AddSingleton<ICosmosDbService>(InitializeCosmosClientInstanceAsync(builder.Configuration.GetSection("CosmosDb")).GetAwaiter().GetResult());

这就是控制器的样子:

namespace demoCosmoDB.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : Controller
    {
        private readonly ICosmosDbService _cosmosDbService;

        public UserController(ICosmosDbService cosmosDbService)
        {
            _cosmosDbService = cosmosDbService ?? throw new ArgumentNullException(nameof(cosmosDbService));
        }
        [HttpGet]
        [Route("{Id:int}")]
        public async Task<IActionResult> GetUser(string Id)
        {
            return Ok(await _cosmosDbService.GetUser(Id));
        }
    }
}

DbService 只是一个关于实现什么的接口:

假设我有另一个控制器 ArticleController,如何使用容器 ID 例如“文章”实例化?

我试过了:

static async Task<CosmosClient> InitializeCosmosClientInstanceAsync(IConfigurationSection configurationSection)
{
    var account = configurationSection["Account"];
    var key = configurationSection["Key"];
    var client = new CosmosClient(account, key);
    return client;
}

但我不知道如何修改其余部分:

builder.Services.AddSingleton<ICosmosDbService>(InitializeCosmosClientInstanceAsync(builder.Configuration.GetSection("CosmosDb")).GetAwaiter().GetResult()); 
....

不要为每个容器创建客户端实例。创建一个 CosmosClient 实例并使用它访问同一帐户中的任何和所有容器。

例如:

static CosmosClient InitializeCosmosClientInstance(IConfigurationSection configurationSection)
{
    var account = configurationSection["Account"];
    var key = configurationSection["Key"];
    var client = new CosmosClient(account, key);
    return client;
}
builder.Services.AddSingleton<CosmosClient>(InitializeCosmosClientInstance(builder.Configuration.GetSection("CosmosDb")));
namespace demoCosmoDB.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : Controller
    {
        private readonly Container _cosmosContainer;

        public UserController(CosmosClient cosmosClient)
        {
            if (cosmosClient == null) {
                throw new ArgumentNullException(nameof(cosmosClient));
            }

            _cosmosContainer = cosmosClient.GetContainer("dbName", "containerName");
            // you can have different containers for different controllers
        }
        [HttpGet]
        [Route("{Id:int}")]
        public async Task<IActionResult> GetUser(string Id)
        {
           // use the Container to perform your operations
        }
    }
}