Alex Gordon
Alex Gordon

Reputation: 60811

how do we bootstrap the cosmosdb client?

how do we use DI to bootstrap the cosmos client at startup?

This is what I have so far:

    services.AddSingleton(s => {
        var connectionString = Configuration["CosmosDBConnection"];
        if (string.IsNullOrEmpty(connectionString))
        {
            throw new InvalidOperationException(
                "Please specify a valid CosmosDBConnection in the appSettings.json file or your Azure Functions Settings.");
        }

        return new CosmosClientBuilder(connectionString)
            .Build();
    });

How do I use this cosmos db client in my code?

Upvotes: 0

Views: 92

Answers (1)

Matias Quaranta
Matias Quaranta

Reputation: 15603

Reference: https://github.com/ealsur/ondotnet-cosmosdb/tree/master/src/episode1/streams

Once you register the object on the DI container, you can just pull it on your Controller declaration:

public class ItemController : Controller
{
    private readonly CosmosClient client;
    public ItemController(CosmosClient cosmosClient)
    {
        this.client = cosmosClient;
    }
}

Upvotes: 1

Related Questions