无法使用 Microsoft.EntityFrameworkCore.Cosmos 连接到 Azure Cosmos Db 帐户 - 响应状态代码

Lui*_*osa 4 azure entity-framework-core azure-cosmosdb

CosmosDb 提供程序正在发送此消息:

“响应状态代码不表示成功:503 子状态:0 原因:(请求失败,因为客户端无法与 1 个区域的 3 个端点建立连接。请检查客户端资源匮乏问题并验证客户端和服务器之间的连接。”

在我的测试中,它有效(.net core 3.1):

Task.Run(async () =>
        {
            var endpoint = “test”;
            var masterKey = “test”;
            using (var client = new DocumentClient(new Uri(endpoint), masterKey))
            {
                //Insert new Document  
                Console.WriteLine("\r\n>>>>>>>>>>>>>>>> Creating Document <<<<<<<<<<<<<<<<<<<");
                dynamic candidato = new
                {
                    Id = 1,
                    Nome = "Test"
                };

                var document1 = await client.CreateDocumentAsync(
                    UriFactory.CreateDocumentCollectionUri("Test", "Test"),
                    candidato);

                Console.ReadKey();
            }

        }).Wait();
Run Code Online (Sandbox Code Playgroud)

它不是:

            Task.Run(async () =>
            {
                using (var context = new StudentsDbContext())
                {
                    context.Add(new FamilyContainer(2, "Test"));
                    await context.SaveChangesAsync();
                }

            }).Wait();

public class FamilyContainer
{
    public int Id { get; set; }
    public string Nome { get; set; }

    public FamilyContainer(int id, string nome)
    {
        Id = id;
        Nome = nome;
    }

}

public class StudentsDbContext : DbContext
{
    public DbSet<FamilyContainer> FamilyContainer { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseCosmos(
           "test",
           "test",
           "FamilyDatabase",
           options =>
           { }
         );
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<FamilyContainer>(x =>
        {
            x.ToContainer("FamilyContainer");
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

套餐

谁能帮我?谢谢

失败:Microsoft.EntityFrameworkCore.Update[10000] 保存上下文类型“...”的更改时,数据库中发生异常。Microsoft.EntityFrameworkCore.Storage.RetryLimitExceededException:使用“CosmosExecutionStrategy”执行数据库操作时超出了最大重试次数 (6)。请参阅最近失败的内部异常。---> Microsoft.Azure.Cosmos.CosmosException:响应状态代码未表示成功:503 子状态:0 原因:(Microsoft.Azure.Documents.ServiceUnavailableException:服务当前不可用。ActivityId:07fbf539-0d44-4e5a-89d0- cd46838ee605, {"RequestStartTimeUtc":"2020-02-21T16:34:09.1834993Z","RequestEndTimeUtc":"2020-02-21T16:34:41.3484203Z":"4020.10C NumberRegionsAttempted":1,"ResponseStatisticsList":[{"ResponseTime":"2020-02-21T16:34:11.5964152Z","ResourceType":2,"OperationType":0,"StoreResult":"StorePhysicalAddress: rntbd:. ../,LSN:-1,GlobalCommittedLsn:-1,PartitionKeyRangeId:,IsValid:True,StatusCode:410,SubStatusCode:0,RequestCharge:0,ItemLSN:-1,SessionToken:,UsingLocalLSN:False,TransportException:客户端传输发生错误:无法连接到远程端点。(时间:2020-02-21T16:34:11.5298608Z,活动ID:07fbf539-0d44-4e5a-89d0-cd46838ee605,错误代码:ConnectFailed [0x0005],基础错误:socket40D000000000000...内部异常堆栈跟踪结束 --- NumberRegionsAttempted":1,"ResponseStatisticsList":[{"ResponseTime":"2020-02-21T16:34:11.5964152Z","ResourceType":2,"OperationType":0,"StoreResult":"StorePhysicalAddress: rntbd:. ../,LSN:-1,GlobalCommittedLsn:-1,PartitionKeyRangeId:,IsValid:True,StatusCode:410,SubStatusCode:0,RequestCharge:0,ItemLSN:-1,SessionToken:,UsingLocalLSN:False,TransportException:客户端传输发生错误:无法连接到远程端点。(时间:2020-02-21T16:34:11.5298608Z,活动ID:07fbf539-0d44-4e5a-89d0-cd46838ee605,错误代码:ConnectFailed [0x0005],基础错误:socket40D000000000000...内部异常堆栈跟踪结束 --- 11.5964152Z","ResourceType":2,"OperationType":0,"StoreResult":"StorePhysicalAddress: rntbd:.../, LSN: -1, GlobalCommittedLsn: -1, PartitionKeyRangeId: , IsValid: True, StatusCode: 410 , SubStatusCode: 0, RequestCharge: 0, ItemLSN: -1, SessionToken: , UsingLocalLSN: False, TransportException: 发生客户端传输错误:无法连接到远程端点。(时间:2020-02-21T16:34:11.5298608Z,活动ID:07fbf539-0d44-4e5a-89d0-cd46838ee605,错误代码:ConnectFailed [0x0005],基础错误:socket40D000000000000...内部异常堆栈跟踪结束 --- 11.5964152Z","ResourceType":2,"OperationType":0,"StoreResult":"StorePhysicalAddress: rntbd:.../, LSN: -1, GlobalCommittedLsn: -1, PartitionKeyRangeId: , IsValid: True, StatusCode: 410 , SubStatusCode: 0, RequestCharge: 0, ItemLSN: -1, SessionToken: , UsingLocalLSN: False, TransportException: 发生客户端传输错误:无法连接到远程端点。(时间:2020-02-21T16:34:11.5298608Z,活动ID:07fbf539-0d44-4e5a-89d0-cd46838ee605,错误代码:ConnectFailed [0x0005],基础错误:socket40D000000000000...内部异常堆栈跟踪结束 --- StatusCode:410,SubStatusCode:0,RequestCharge:0,ItemLSN:-1,SessionToken:,UsingLocalLSN:False,TransportException:发生客户端传输错误:无法连接到远程端点。(时间:2020-02-21T16:34:11.5298608Z,活动ID:07fbf539-0d44-4e5a-89d0-cd46838ee605,错误代码:ConnectFailed [0x0005],基础错误:socket40D000000000000...内部异常堆栈跟踪结束 --- StatusCode:410,SubStatusCode:0,RequestCharge:0,ItemLSN:-1,SessionToken:,UsingLocalLSN:False,TransportException:发生客户端传输错误:无法连接到远程端点。(时间:2020-02-21T16:34:11.5298608Z,活动ID:07fbf539-0d44-4e5a-89d0-cd46838ee605,错误代码:ConnectFailed [0x0005],基础错误:socket40D000000000000...内部异常堆栈跟踪结束 ---

Pra*_*ngh 8

我面临同样的问题。

对我有用的ConnectionModeConnectionMode.Gateway在初始化时更改为CosmosClient

var options = new CosmosClientOptions() { ConnectionMode = ConnectionMode.Gateway };
var client = new CosmosClient(endpoint, key, options); 
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请参阅:

https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.cosmos.cosmosclientoptions?view=azure-dotnet

https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.cosmos.connectionmode?view=azure-dotnet

  • 就我而言,我在代理后面运行软件,“ConnectionMode = ConnectionMode.Gateway”就成功了 (2认同)

Mat*_*nta 7

TransportException:发生客户端传输错误:无法连接到远程端点。(时间:2020-02-21T16:34:11.5298608Z,活动ID:07fbf539-0d44-4e5a-89d0-cd46838ee605,错误代码:ConnectFailed [0x0005],基础错误:socket错误ConnectionRefused

这意味着连接被拒绝。

  • 您的 Cosmos DB 帐户启用了防火墙/VPN,并且由于不在白名单 IP/网络中,应用程序无法建立连接:尝试检查您的帐户配置。
  • 您正在执行代码的环境限制了连接(某些公司防火墙或网络可能会阻止端口范围):尝试在不同的网络中运行应用程序,或使用GatewayMode。如果可行,则这与网络有关。
  • 机器可能在套接字上运行低或在 CPU 上运行高。