Attempting to connect to azure database has sudden

2019-05-31 15:26发布

问题:

I have the following code running in an Azure Function (V2);

 new DocumentClient(new Uri(keys.dbEndPoint), keys.dbPrimaryKey);

Which has worked fine running in azure functions on localhost until today, without changing anything I'm now getting the following exception:

Data [IDictionary]:{System.Collections.ListDictionaryInternal} HResult [int]:-2146233036 HelpLink [string]:null InnerException [Exception]:{System.NullReferenceException: Object reference not set to an instance of an object.} Message [string]:"The type initializer for 'Microsoft.Azure.Documents.UserAgentContainer' threw an exception." Source [string]:"Microsoft.Azure.DocumentDB.Core" StackTrace [string]:" at Microsoft.Azure.Documents.UserAgentContainer..ctor()\r\n at Microsoft.Azure.Documents.Client.ConnectionPolicy..ctor()\r\n at Microsoft.Azure.Documents.Client.ConnectionPolicy.get_Default()\r\n
at Microsoft.Azure.Documents.Client.DocumentClient.Initialize(Uri serviceEndpoint, ConnectionPolicy connectionPolicy, Nullable1 desiredConsistencyLevel)\r\n at Microsoft.Azure.Documents.Client.DocumentClient..ctor(Uri serviceEndpoint, String authKeyOrResourceToken, ConnectionPolicy connectionPolicy, Nullable1 desiredConsistencyLevel)\r\n at Platform.AzureConfig.get_Database() in D:\DeansCloud\AzureConfig.cs:line 60" TargetSite [MethodBase]:{Void .ctor()} TypeName [string]:"Microsoft.Azure.Documents.UserAgentContainer" Static members Non-Public members

Which doesn't say much, the inner exception mentions a null reference exception:

Message [string]:"Object reference not set to an instance of an object."

Is this an issue with azure cosmos db now? Do I need to update something?

回答1:

I found the issue to be the latest azure core tools, downgrading to version 24 fixed it.



回答2:

Instead of creating your own DocumentClient, try to use the binding-based version. This will also give you better performance. See an example here or here.