10

Azure application settings (for azure function) has a option for a DocumentDB connection string

Anyone have any idea how this should be populated/formatted?

i currently use:

var documentDbEndpointUri = new Uri(ConfigurationManager.AppSettings["DocumentDbEndpointUri"]);
            var documentDbAuthKey = ConfigurationManager.AppSettings["DocumentDbAuthKey"];
            return new DocumentClient(documentDbEndpointUri, documentDbAuthKey);

Although I'd like to switch to a single value connection string.

John
  • 754
  • 1
  • 10
  • 26
  • 1
    Can you try something like `AccountEndpoint=https://accountname.documents.azure.com:443/;AccountKey=accountkey==;`? – Gaurav Mantri Jul 29 '17 at 06:52
  • So I looked around and it seems even if you provide connection string like this, it won't be of any help to you as `DocumentClient` does not have an overload that takes connection string. Also, there's no parser method in the library. Closest I could find was here: https://stackoverflow.com/questions/41683369/documentdb-net-client-using-connection-string. – Gaurav Mantri Jul 29 '17 at 07:39

2 Answers2

26

Try AccountEndpoint=https://accountname.documents.azure.com:443/‌​;AccountKey=accountk‌​ey==;Database=database

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
hsedidin
  • 452
  • 3
  • 8
  • 2
    The OP's issue isn't with finding the connection string. It's with using it as a parameter. – David Makogon Jul 30 '17 at 18:55
  • 4
    @hseddin Find this as very useful when I'm trying to find the connection string which is needed for Migration Tool. Every documentation just error-prone and not complete. – Kannaiyan Apr 09 '18 at 16:31
  • For me Migration Tool gives an error when having `/` at the end of `https://accountname.documents.azure.com:443/‌​`. So use `https://accountname.documents.azure.com:443` instead – AlbertK Aug 26 '21 at 19:15
2

Firstly, as @Gaurav Mantri said in comment, currently DocumentClient does not have constructor overloads using connection string, you cannot directly use a connection string to create an instance of DocumentClient even if you provide/add connection string for DocumentDB in Azure application settings.

enter image description here

Note: here is a feedback for this issue, if you have same feature request, you can vote for it.

Secondly, If you’d like to access the DocumentDB service via DocumentClient, you can add both DocumentDbEndpointUri and DocumentDbAuthKey in App settings, and then read them in function code.

var serviceEndpoint = System.Configuration.ConfigurationManager.AppSettings["DocumentDbEndpointUri"];
var authKey = System.Configuration.ConfigurationManager.AppSettings["DocumentDbAuthKey"];

//or
//var serviceEndpoint = Environment.GetEnvironmentVariable("DocumentDbEndpointUri");
//var authKey = Environment.GetEnvironmentVariable("DocumentDbAuthKey");
Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Fei Han
  • 26,415
  • 1
  • 30
  • 41
  • I wrote a helper that can create a DocumentClient off connection string here https://github.com/Azure/azure-documentdb-dotnet/issues/203. However, if you set the connection string dropdown to Document DB, it does not replace the connection string in the web.config file. You must leave it Custom for it to work. Not sure what that dropdown is doing to break things. – MPavlak Sep 28 '17 at 17:26