Cannot initialize connection to MongoDB using Fastendpoints project template

21 Views Asked by At

I created a new project using a Fastendpoints web app template in Visual Studio 2022. It is a .net core 8.0 app that comes with an embedded notification service using a mongodb data store. I created a MongoDB account and have a database created in the cloud. I can successfully access my database via the mongodb web UI. I copied the connection string from the mongo dev portal and placed it in the app but I am unable to successfully run the app now. The mongodb WILL NOT connect successfully to my mongo database and that is blocking my ability run the app.

I installed mongodb-atlas locally and then attempted to create a local database only to have it tell me that local mongodb-atlas is not compatible with Windows OS. The initasync() call in program.cs seems to be inserting a port address into my connection string as though I'm connecting to localhost even though on Windows mongodb will not host. Here is a sample of what I am attempting, of course my username/pw removed:

await DB.InitAsync(myDbName, "mongodb://<MyUserName>:<MyPassword>@olaxxwh.mongodb.net/?authSource=admin");

My call to this gives me the following error:

System.ArgumentException: 'mongodb://<MyUserName>:<MyPassword>@olaxxwh.mongodb.net/?authSource=admin:27017' 

is not a valid end point. (Parameter 'value')'

Notice the :27017 added into the connection string by the init function. I have also tried the mongodb+srv as well as mongodb but it makes no difference.

I have been battling this for 2 days. I do not understand why this is so freaking complicated. What is the magic to get initasync() using MongoDB.Entities to work??

1

There are 1 best solutions below

0
Woodman On

I figured out the solution. There is an overload on DB.InitAsync() that accepts a DbName and MongoClientSettings value. If you have a MongoDB-Atlas cloud database, pull the connection string from the MongoDB dev portal, insert your user/pw where specified, then do the following in your Fastendpoints program.cs file it will work!

settings = MongoClientSettings.FromConnectionString(YourConnectionString);

settings.ServerApi = new ServerApi(ServerApiVersion.V1);

await DB.InitAsync(dbName, settings);

Enjoy!