Azure Cosmos DB 客户端在下面抛出 "HttpRequestException: attempt was made to access a socket in a way forbidden by its access permissions"

标签 azure azure-functions azure-cosmosdb asp.net-core-3.1 azure-cosmosdb-sqlapi

我在 Azure Durable Function 的 ASP.NET Core 3.1 中使用 SDK Microsoft.Azure.Cosmos 3.28.0 中的 CosmosClient。该客户端正在从我的 cosmos 实例(核心(SQL))获取数据并向其发送数据,它工作正常,但我发现它在以下元数据的 http 请求中不断抛出异常

获取 169.254.169.254/元数据/实例

System.Net.Http.HttpRequestException:尝试以访问权限禁止的方式访问套接字。

我使用以下配置:

  private static void RegisterCosmosDbClient(ContainerBuilder builder)
        {
            builder.Register(c => new SocketsHttpHandler()
            {
                PooledConnectionLifetime = TimeSpan.FromMinutes(10), // Customize this value based on desired DNS refresh timer
                MaxConnectionsPerServer = 20, // Customize the maximum number of allowed connections
            }).As<SocketsHttpHandler>().SingleInstance();

            builder.Register(
                    x =>
                    {
                        var cosmosDbOptions = x.Resolve<CosmosDbOptions>();
                        var socketsHttpHandler = x.Resolve<SocketsHttpHandler>();
                        return new CosmosClient(cosmosDbOptions.ConnectionString, new CosmosClientOptions()
                        {
                            ConnectionMode = ConnectionMode.Direct,
                            PortReuseMode = PortReuseMode.PrivatePortPool,
                            IdleTcpConnectionTimeout = new TimeSpan(0, 23, 59, 59),
                            SerializerOptions = new CosmosSerializationOptions()
                            {
                                PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase
                            },
                            HttpClientFactory = () => new HttpClient(socketsHttpHandler, disposeHandler: false)
                        });
                    })
                .AsSelf()
                .SingleInstance();
   }

我还尝试了传递 IHttpClientFactory from this blog 的方法但这没有帮助。

最佳答案

您的环境中似乎没有可用的新套接字,因此您收到套接字禁止错误。请查看如何manage connection for Azure Cosmos DB clients你应该use a singleton Azure Cosmos DB client for the lifetime of your application解决问题。如果您仍然面临利用单例对象的问题,请告诉我,以便我们进一步审查。

关于Azure Cosmos DB 客户端在下面抛出 "HttpRequestException: attempt was made to access a socket in a way forbidden by its access permissions",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73235961/

相关文章:

azure - Windows 8/RT + Phone 8 应用程序 - 同步数据

c# - 具有事务的长时间运行作业 Azure 服务总线的模式

azure - 优雅地停止 Azure 容器实例

c# - ServiceBusTrigger 选择错误的配置文件

azure - DocumentDb 客户端接收无效的 api 版本 documentdb 确保传递有效的 x-ms-version header 值

azure - Blob 存储 GZRS 复制的 DR 测试

azure-functions - 使用 Azure 数据工厂将 csv 文件、文本文件、pdf 文件转换为 json

c# - 在 Azure 函数中序列化

c# - DocumentDb CreateDocumentQuery 停止工作

c# - Cosmos DB 中的并行更新数据