Share via

dotnet function app: gremlin.net.driver with cosmosdb throw exception: Cannot connect remote server

Zehou Zhang 41 Reputation points
2022-02-05T12:51:43.46+00:00

171612-image.png

Function app throw "cannot connect remote server" exception sometimes while send gremlin query to cosmosDB
Following are code where throw exception, anyone knows why this happen?

/// <summary>  
            ///    Execute CosmosDB graph command   
            /// </summary>  
            public virtual async Task<dynamic> executeCmnd(string command)  
            {  
                 
                var gremlinServer = new GremlinServer(_dbConfig.HostName, _dbConfig.Port, enableSsl: true, username: "/dbs/" + _dbConfig.Database + "/colls/" + _dbConfig.Collection, password: _dbConfig.AuthKey);  
                using (var gremlinClient = new GremlinClient(gremlinServer, new GraphSON2Reader(), new GraphSON2Writer(), GremlinClient.GraphSON2MimeType))  
                {  
                    return await gremlinClient.SubmitAsync<dynamic>(command);  
                }  
            }  
  

    
Azure Functions
Azure Functions

An Azure service that provides an event-driven serverless compute platform.

Azure Cosmos DB
Azure Cosmos DB

An Azure NoSQL database service for app development.

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

MayankBargali-MSFT 71,016 Reputation points Moderator
2022-02-07T08:06:07.873+00:00

@Zehou Zhang As per the error, it looks like the socket exception and the reason for this error is that you reached the limit of the maximum available socket at your function app and sandbox disabled access for new sockets.

As per your code, I am not sure if you are calling executeCmnd method on every invocation. If this is the case then the suggestion is not to create a new connection to the Cosmo DB for every request coming to your function. You should only create the connection once and reuse it.
I will suggest you to review managing cosmo DB clients as mentioned in this document and define the static client object while creating the connection.

You can define the below line of code as static and outside your function app invocation but within the class.

         var gremlinServer = new GremlinServer(_dbConfig.HostName, _dbConfig.Port, enableSsl: true, username: "/dbs/" + _dbConfig.Database + "/colls/" + _dbConfig.Collection, password: _dbConfig.AuthKey);  

Feel free to get back to me if you need any assistance.

Was this answer helpful?


1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 84,086 Reputation points
    2022-02-05T17:05:10.037+00:00

    The connection error could be

    You have the invalid host name
    Firewall rules block connection

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.