Search code examples
c#.netazureazure-cosmosdb

Specify the connection port to Azure Cosmos db (SQL)


I am using Azure Cosmos DB in the office network, and the application doesn't work when I'm trying to connect to the Azure Cosmos DB, but the application works fine when I'm connecting from the home network. The issue is that some ports are blocked in the office network, and based on this, the port can be any of this range (10000 - 20000)

I'm using Microsoft.Azure.Cosmos package. Is there any way I can specify which port I want to use to get the data from Azure Cosmos DB? This way, we can open only some ports (not the whole range between 10000 to 20000)


Solution

  • You have two choices:

    • talk to your network admin to open up specific ports, or
    • switch your connection mode from 'Direct/tcp' to 'Gateway/https', which will then connect via port 443.

    You can select Gateway mode when building your Cosmos DB client:

    var client = new CosmosClient(connectionString,
    new CosmosClientOptions
    {
        ConnectionMode = ConnectionMode.Gateway
    });
    

    You should also evaluate the connection choice of Gateway/https vs Direct/tcp, to understand the potential differences (e.g. latency).

    You cannot arbitrarily choose the port Cosmos DB uses.

    FYI all Cosmos DB SDKs support Gateway/https. The .NET SDK (and Java SDK) also support Direct/tcp.