How to add CosmosDB to .Net 6 WebAPI?
Asked Answered
J

2

11

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?

Jocosity answered 7/11, 2021 at 11:52 Comment(1)
As far as I know it is only EF Core that has such a method to wire things up. I've ended up with my own wrappers anyway (for diagnostic logging). As EF Core has some CosmosDB support – with pretty sever limitations on how CosmosDB is used – adding EF Core could get you to CosmosDB...Graeco
B
14

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

Bermuda answered 8/11, 2021 at 7:42 Comment(6)
The HttpClientFactory and CosmosClientOptions are not used in your example nor neededOthilia
@MatiasQuaranta I have commented HttpClientFactory and CosmosClientOptions , it's just a sample code, op can refer to it. Tks for your advice.Bermuda
Please note as of today this is not a solution for production.Breann
Did you have any problem with connection string? Things like adding the account endpoint?Quadricycle
@Kentonbmax, why is this is not suitable for production? do you have a solution that is more appropriate for production environments?Anticipatory
I know this is an old thread, but I'm researching the same thing now in early 2024 and I tend to favor this approach because the only thing that gets injected into controllers is the CosmosClient. But to answer the previous question, I dont 'think this example is production-ready because the database name and container name are hard-coded in the controller action. Those should really be settings pulled from configuration. IConfiguration should be injected into the controller. Just my two cents.Odoacer
B
3

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
Blub answered 16/12, 2021 at 10:48 Comment(1)
did you use a file for configurationSection? Could you provide more details?Quadricycle

© 2022 - 2024 — McMap. All rights reserved.