[英]Azure Cosmos DB - How to instantiate Cosmos Db with a different container Id in different controllers
我正在使用 .net 6
我正在嘗試使用 CosmosDb 並且我正在學習本教程。 問題是它們僅基於在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());
這就是 controller 的樣子:
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 只是一個關於實現什么的接口:
假設我有另一個 controller ArticleController,如何使用 contaienr id 例如“文章”實例化?
我試過了:
static async Task<CosmosClient> InitializeCosmosClientInstanceAsync(IConfigurationSection configurationSection)
{
var account = configurationSection["Account"];
var key = configurationSection["Key"];
var client = new CosmosClient(account, key);
return client;
}
但是不知如何修改rest:
builder.Services.AddSingleton<ICosmosDbService>(InitializeCosmosClientInstanceAsync(builder.Configuration.GetSection("CosmosDb")).GetAwaiter().GetResult());
....
請不要為每個 Container 創建一個客戶端實例。 創建單個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
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.