gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:55:00 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com