2

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.

jophab
  • 5,356
  • 14
  • 41
  • 60
Duc Nguyen
  • 530
  • 8
  • 16
  • In the 2.x version of the .NET SDK, you can pass a CancellationToken which would allow you to cancel the operation. Today, you cannot handle this remote error in the SDK itself. We've got a new version of the SDK we're working on that will let you add a handler that would customize the handling of this error. – Chris Anderson Nov 29 '18 at 19:06
  • If you send an email to AskCosmosDB@microsoft.com, you can get ahold of the engineering team. There is probably a feature we could add which would make this fail faster, even without a custom handler. – Chris Anderson Nov 29 '18 at 19:08
  • Thanks!, It's so bad :( My app is running and cannot stop, so You think I can use timeout on my code :( – Duc Nguyen Nov 30 '18 at 11:20

1 Answers1

1

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

jophab
  • 5,356
  • 14
  • 41
  • 60