Efcore: Unable to connect to Azure Cosmos Db Account (Core SQL API)

Created on 18 Oct 2019  路  4Comments  路  Source: dotnet/efcore

I'm unable to connect to an Azure CosmosDB with the CosmosDb provider using the following packages:

Microsoft.EntityFrameworkCore 3.0.0
Microsoft.EntityFrameworkCore.Cosmos 3.0.0

Getting this message back:

Response status code does not indicate success: 503 Substatus: 0 Reason: (The request failed because the client was unable to establish connections to 3 endpoints across 1 regions. Please check for client resource starvation issues and verify connectivity between client and server.

Stacktrace

at Microsoft.Azure.Cosmos.ResponseMessage.EnsureSuccessStatusCode() at Microsoft.Azure.Cosmos.CosmosResponseFactory.ToObjectInternal[T](ResponseMessage cosmosResponseMessage, CosmosSerializer jsonSerializer) 
at Microsoft.Azure.Cosmos.CosmosResponseFactory.<>c__DisplayClass6_0.<CreateDatabaseResponseAsync>b__0(ResponseMessage cosmosResponseMessage) 
at Microsoft.Azure.Cosmos.CosmosResponseFactory.<ProcessMessageAsync>d__121.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Microsoft.Azure.Cosmos.CosmosClient.<CreateDatabaseIfNotExistsAsync>d__40.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult()
at Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal.CosmosClientWrapper.d__13.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal.CosmosClientWrapper.CreateDatabaseIfNotExistsOnce(DbContext context, Object state) 
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementation[TState,TResult](Func3 operation, Func3 verifySucceeded, TState state) 
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func3 verifySucceeded) 
at Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal.CosmosClientWrapper.CreateDatabaseIfNotExists() at Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal.CosmosDatabaseCreator.EnsureCreated() 
at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated() at EFCoreCosmosDbTest.Test.<Run>d__0.MoveNext() in C:\Git\EFCoreCosmosDbTest\EFCoreCosmosDbTest\Test.cs:line 26 
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at EFCoreCosmosDbTest.Program.<Main>d__0.MoveNext()
in C:\Git\EFCoreCosmosDbTest\EFCoreCosmosDbTest\Program.cs:line 9

If I change to the Microsoft.Azure.DocumentDB.Core 2.8.1 and use the DocumentClient I have no issues connecting to the same CosmosDb account in Azure and create my database, container etc. from the same solution.

Also, if I download the .net core quickstart project from Azure, it works without issues.

Note, that It works fine connecting to a the Azure Cosmos Db Emulator locally with EF.

I used the https://github.com/aspnet/EntityFramework.Docs/tree/master/samples/core/Cosmos repo to run the test.

Any ideas what I'm doing wrong?

Further technical details

EF Core version: 3.0
Database provider: Microsoft.EntityFrameworkCore.CosmosDb
Target framework: .NET Core 3.0
Operating system: Windows 10 Ent
IDE: Visual Studio 2019 16.3

closed-question customer-reported

Most helpful comment

C# .UseCosmos( "serviceEndPoint", "authKeyOrResourceToken", "databaseName", o => { o.ConnectionMode(ConnectionMode.Gateway); });

All 4 comments

@niklasbjorklund Try configuring ConnectionMode as Gateway in the UseCosmos call. If that doesn't work try using the Cosmos SDK v3.x directly without EF Core

@AndriySvyryd Thanks, it worked changing the ConnectionMode to Gateway. Strange though, since I'm pretty sure I tried that before posting the issue. Anyhow it works now :)

@niklasbjorklund Try configuring ConnectionMode as Gateway in the UseCosmos call. If that doesn't work try using the Cosmos SDK v3.x directly without EF Core

@AndriySvyryd Thanks, it worked changing the ConnectionMode to Gateway. Strange though, since I'm pretty sure I tried that before posting the issue. Anyhow it works now :)

@niklasbjorklund Try configuring ConnectionMode as Gateway in the UseCosmos call. If that doesn't work try using the Cosmos SDK v3.x directly without EF Core

Hi, how to do this? I can't seem to make it work... do you have a sample code?

C# .UseCosmos( "serviceEndPoint", "authKeyOrResourceToken", "databaseName", o => { o.ConnectionMode(ConnectionMode.Gateway); });

Was this page helpful?
0 / 5 - 0 ratings