Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DocumentDB .Net client using connection string

I checked the MSDN on DocumentDB for .Net (here) and found 3 valid constructors. However none of them makes use of connection strings, which sounds strange for me.

Is there seriously no way to instantiate client with connection string instead of endpoint+authKey combo or I'm missing something?

For example majority of other Microsoft services uses this concept, i.e. https://docs.microsoft.com/en-us/azure/storage/storage-configure-connection-string#parsing-a-connection-string . In our case it would be super if all Azure related stuff is initialized in same manner. Just cleaner, not something show-stopping.

P.S. Please stop telling me about existing constructors with Uri and authKey parameters, question is (slightly) different. I can follow links I provided myself, no needs to help. Thanks.

like image 317
Sanctus Avatar asked Jan 16 '17 19:01

Sanctus


2 Answers

You can actually do this in a roundabout way.

internal class CosmosDBConnectionString
{
    public CosmosDBConnectionString(string connectionString)
    {
        // Use this generic builder to parse the connection string
        DbConnectionStringBuilder builder = new DbConnectionStringBuilder
        {
            ConnectionString = connectionString
        };

        if (builder.TryGetValue("AccountKey", out object key))
        {
            AuthKey = key.ToString();
        }

        if (builder.TryGetValue("AccountEndpoint", out object uri))
        {
            ServiceEndpoint = new Uri(uri.ToString());
        }
    }

    public Uri ServiceEndpoint { get; set; }

    public string AuthKey { get; set; }
}

Then

var cosmosDBConnectionString = new CosmosDBConnectionString(connectionString)
var client = new DocumentClient(
            cosmosDBConnectionString.ServiceEndpoint,
            cosmosDBConnectionString.AuthKey)

This is taken from the Azure WebJobs Extensions SDK, which is how Azure Functions V2 is able to work with just a connection string. Saves having to try and parse the string yourself.

like image 52
deadwards Avatar answered Sep 20 '22 07:09

deadwards


The DocumentDB SDKs do not have constructor overloads using connection strings. They support initializing with endpoint + master key, and endpoint + permissions/resource tokens.

If you'd like to see a single connection string argument, please propose/upvote here: https://feedback.azure.com/forums/263030-documentdb

like image 36
Aravind Krishna R. Avatar answered Sep 22 '22 07:09

Aravind Krishna R.