Reputation: 588
I am using .NET SDK Microsoft.Azure.DocumentDB
to work with cosmos db on Azure.
I've a problem when my network cannot connect to cosmos db (proxy, network, ...)
Example:
this.client = new DocumentClient(new Uri(EndpointUrl), PrimaryKey, new ConnectionPolicy {
ConnectionMode = ConnectionMode.Direct, ConnectionProtocol = Protocol.Tcp,
RetryOptions = new RetryOptions {MaxRetryAttemptsOnThrottledRequests = 5,
MaxRetryWaitTimeInSeconds= 5 },
RequestTimeout = new TimeSpan(1000)});
var a = await this.client.CreateDatabaseIfNotExistsAsync(new Database { Id = "FamilyDB" });
My application loop forever in line CreateDatabaseIfNotExistsAsync
and with message:
Endpoint not reachable. Refresh cache and retry
Failover happening. retryCount 6
RefreshLocationAsync() refreshing locations
An error occurred while sending the request. ---> System.Net.WebException: The remote server returned an error: (407) Proxy Authentication Required.
Endpoint not reachable. Refresh cache and retry
Failover happening. retryCount 7
RefreshLocationAsync() refreshing locations
An error occurred while sending the request. ---> System.Net.WebException: The remote server returned an error: (407) Proxy Authentication Required.
....
I cannot stop or try catch it, I tried requestoption
, requestTimeout
but it is not working.
So if I cannot connect to cosmos db, how can i stop it or try catch it.
Upvotes: 2
Views: 1595
Reputation: 5509
You can use the method OpenAsync()
on documentClient
object to check whether
it was initialized successfully.
It will throw exceptions for End Point not reachable
as well as Invalid Authorization Key
DocumentClient documentClient = new DocumentClient(new Uri(endPointUri), authKey);
try
{
await documentClient.OpenAsync();
}
catch(Exception ex)
{
// Handle Exception Here
}
You can proceed with querying on successful initialization of DocumentClient
Upvotes: 1