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.