skip to Main Content

I use CosmosClient from SDK Microsoft.Azure.Cosmos 3.28.0 in ASP.NET Core 3.1 in Azure Durable Function. This client is getting and sending data from/to my cosmos instance (Core (SQL)) and it works fine but I see that it constantly throws exception in following http request for metadata

GET 169.254.169.254/metadata/instance

System.Net.Http.HttpRequestException: An attempt was made to access a socket in a way forbidden by its access permissions.

I use following configuration:

  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();
   }

I also tried approach with passing IHttpClientFactory from this blog but it didn’t help.

2

Answers


  1. That particular IP and path is for https://learn.microsoft.com/azure/virtual-machines/windows/instance-metadata-service?tabs=windows

    The SDK is attempting to detect the Azure information. It could mean for Durable Functions, this information and endpoint is not available.

    This does not affect SDK operations and should not block you from performing other actions on the CosmosClient instance.

    Login or Signup to reply.
  2. It looks like there are no new sockets available in your environment therefore you are getting the socket forbidden error. Please review how to manage connection for Azure Cosmos DB clients and you should use a singleton Azure Cosmos DB client for the lifetime of your application to resolve the issue. In case if you still facing the issue leveraging the singleton object then please let me know so we can further review it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search