Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create if not exists methods not available

I have a .Net core app that I have installed the cosmos nuget package into:

Install-Package Microsoft.Azure.DocumentDB.Client

I am attempting to follow the instructions given here from MS that details how to create a database and the collections.

Using dependency injection, I have registered IDocumentClient. However, when I attempt to use this in a setup class to create the database and collections, the following methods are not listed as avaialable:

public void Setup(IDocumentClient client)
{
    await client.CreateDatabaseIfNotExistsAsync(new Database() { Id = databaseId });

    await client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(databaseId), new DocumentCollection { Id = "Identity" });
}

In both cases, the methods flag up as not known, and the IDE prompts me to create them. This happens even with the Microsoft.Azure.Documents.Client namespace defined in the file.

like image 462
Obsidian Phoenix Avatar asked Feb 21 '26 16:02

Obsidian Phoenix


1 Answers

Whilst these methods exist on the DocumentClient class, they are not actually part of the IDocumentClient interface.

In this manner, you will need to pass the concrete DocumentClient class into the setup method, rather than the IDocumentClient interface.

public void Setup(DocumentClient client)
{
    await client.CreateDatabaseIfNotExistsAsync(new Database() { Id = databaseId });

    await client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(databaseId), new DocumentCollection { Id = "Identity" });
}

Once you do this, the methods are available as normal.

Whilst Dependency Injection/IoC generally prefers the use of Interfaces over concrete classes, it is probably ok in this instance, since the class purpose is specifically to set up and configure the database: a certain knowledge of the underlying technology here is to be expected.

like image 173
Obsidian Phoenix Avatar answered Feb 24 '26 06:02

Obsidian Phoenix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!