How to add CosmosDB to .Net 6 WebAPI?

Viewed 2905

I want to use a CosmosDB SQL-API in a .Net 6 WebAPI. Is there also an AddCosmos() method available to inject client in different services or is it really necessary to implement my own cosmos-service with an interface to inject a client to my own service-classes?

2 Answers

In .Net 6.0, you should add `CosmosClient` in your `Program.cs` file like below sample code.

using Microsoft.Azure.Cosmos;
using Microsoft.OpenApi.Models;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new() { Title = "WebApplication1", Version = "v1" });
}); 
builder.Services.AddHttpClient();
builder.Services.AddSingleton<CosmosClient>(serviceProvider =>
{
    //IHttpClientFactory httpClientFactory = serviceProvider.GetRequiredService<IHttpClientFactory>();

    //CosmosClientOptions cosmosClientOptions = new CosmosClientOptions
    //{
        //HttpClientFactory = httpClientFactory.CreateClient,
        //ConnectionMode = ConnectionMode.Gateway
    //};

    return new CosmosClient("<cosmosdb_connectionstring>");
    // sample code
    //return new CosmosClient("AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==");//, cosmosClientOptions);
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (builder.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
    app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApplication1 v1"));
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

Then you can inject CosmosClient to your controller.

using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Cosmos;
using Microsoft.WindowsAzure.Storage.Table;

namespace WebApplication1.Controllers;
[ApiController]
[Route("[controller]")]
public class HomeController : ControllerBase
{
    public CosmosClient _client;
    private readonly ILogger<WeatherForecastController> _logger;

    public HomeController(ILogger<WeatherForecastController> logger, CosmosClient client)
    {
        _logger = logger;
        _client = client;
    }
    [HttpGet]
    public async Task<string> getvalue() {
        string dbname = "test";
        string containername = "container1";
        Database database = await _client.CreateDatabaseIfNotExistsAsync(dbname);
        Container container = database.GetContainer(containername);
        var query = container.GetItemQueryIterator<Test>("SELECT c.id FROM c");
        string ss = string.Empty;
        while (query.HasMoreResults)
        {
            FeedResponse<Test> result = await query.ReadNextAsync();
            foreach (var item in result) {
                ss += item.id;
            }
        }
        return ss;
    }
    public class Test : TableEntity { 
        public int id { get; set; }
    }
}

Test Result

value in my cosmosdb.

enter image description here

get value by api

enter image description here

This is what I did.

program.cs

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

//Start adding Congiguration for cosmos db
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 Microsoft.Azure.Cosmos.CosmosClient(account, key);
    var database = await client.CreateDatabaseIfNotExistsAsync(databaseName);
    await database.Database.CreateContainerIfNotExistsAsync(containerName, "/id");
    var cosmosDbService = new CosmosDbService(client, databaseName, containerName);
    return cosmosDbService;
}

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

//end
Related