skip to Main Content

I am creating a serverless API for a small company.

At the moment my network access is set to the IP-address 0.0.0.0.
Connection with MongoDB Compass is working.

Function:

[FunctionName("Klanten2")]
    public static async Task<IActionResult> Klanten2(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
    ILogger log)
    {
        try
        {

            MongoClient client = new MongoClient("mongodb+srv://<Username>:<password>@testdb.cbbhf9u.mongodb.net/?retryWrites=true&w=majority");
            var database = client.GetDatabase("Test");
            var collection = database.GetCollection<Klant>("Test");


            return new OkObjectResult(collection);
        }
        catch (Exception ex)
        {
            return new BadRequestObjectResult("Error get klanten - " + ex.Message);
        }


    }

At the moment my connection url is in the function but if it works then I gone move the url to local.settings.json

Error:

An unhandled host error has occurred.
[2023-04-30T17:34:21.622Z] Newtonsoft.Json: Error getting value from ‘DirectConnection’ on ‘MongoDB.Driver.Core.Clusters.ClusterDescription’. MongoDB.Driver.Core: DirectConnection cannot be used when ConnectionModeSwitch is set to UseConnectionMode.

Some one that knows te problem?

Used mongodb before with .Net Core WebApi and everything was working fine.

2

Answers


  1. This error DirectConnection cannot be used when ConnectionModeSwitch is set to UseConnectionMode says that you use 2 mutually exclusive options: DirectConnection and ConnectionMode in MongoClientSettings or connectionString or you messed up a default configuration by assigning ConnectionModeSwitch directly. The code you provided doesn’t set it, so I assume you’ve provided not all code. You should make some additional research to find where it’s assigned.

    Login or Signup to reply.
  2. The error message suggests that there may be a conflict between the connection mode settings and the direct connection settings in the MongoDB driver configuration.

    To resolve the issue, you can try changing the connection mode to Direct instead of using the UseConnectionMode setting. You can modify your code as follows:

    MongoClientSettings settings = MongoClientSettings.FromUrl(new MongoUrl("mongodb+srv://<Username>:<password>@testdb.cbbhf9u.mongodb.net/"));
    
    settings.ConnectionMode = ConnectionMode.Direct;
    
    MongoClient client = new MongoClient(settings);
    var database = client.GetDatabase("Test");
    var collection = database.GetCollection<Klant>("Test");
    
    return new OkObjectResult(collection);
    

    By setting the connection mode to ConnectionMode.Direct, you are telling the driver to use direct connections instead of switching between Automatic and Direct connection modes.

    You may also want to check if there are any other configuration settings that may be causing conflicts with the DirectConnection setting.

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