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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With