MongoConfigurationException when SRV connection string - c#

Driver Language - C#
Driver Version - 2.7.0
DB Version - 4.0.0
.NET Framework 4.6.1
Exception message (credentials and server removed)
The connection string
'mongodb+srv://USER:PASS#uat-xxxx.mongodb.net/test?retryWrites=true'
is not valid.
Code
var client = new MongoClient(#"mongodb+srv://USER:PASS#uat-xxxx.mongodb.net/test?retryWrites=true");
User and password contain no special characters
No connection attempt is made to the server.
However, this works fine if i use the URI version
var client = new MongoClient(#"mongodb://USER:PASS#uat-shard-00-00-xxxxx.mongodb.net:27017,uat-shard-00-01-xxxxx.mongodb.net:27017,uat-shard-00-02-xxxxx.mongodb.net:27017/test?ssl=true&replicaSet=UAT-shard-0&authSource=admin&retryWrites=true");
I've tried to decipher the validation rules from the source but it's beyond my regex ability.
The srv connection string looks fine to me and is exactly the same as that presented to me in the Mongo Atlas UI.
Any idea what i'm doing wrong?
Thanks

I attempted to reproduce this using
var client = new MongoClient("mongodb+srv://USER:PASS#cluster0-xxxx.mongodb.net/test?retryWrites=true");
var dbs = await client.ListDatabaseNames().ToListAsync();
Console.WriteLine(dbs);
Which gave me the expected output (2 database names).
My test environment is .NET 4.6.1, LINQPad, and C# Driver 2.7.0.
You can try eliminating the C# Driver (and framework) from the equation by using the Mongo Shell to test. If you're using Mongo Shell version 3.6 or later, you can test the connection with a command similar to
mongo "mongodb+srv://cluster0-xxxx.mongodb.net/test" --username USER
That being said, you should be able to contact MongoDB Support for your atlas cluster, using the "Support" link on the left side of the Atlas UI. They should be able to help.

Related

Accessing DataVerse data and running SQL statements against it from C# code on .NET 6 (Azure Function)

I'm working with Dynamics365 CE in the cloud. I'm trying to run some rather involved queries that I've built up as SQL scripts (using the wonderful "SQL-4-CDS" plugin for the XrmToolBox).
Now I know I can connect to the Dataverse data store through the TDS endpoint (if enabled - it is in my case), and from SSMS, it works just fine:
Server Name = myorg.crm4.dynamics.com,5558
Authentication = Azure Active Directory - Password
User Name = my company e-mail
I can connect to Dataverse, and run my queries - all is great.
Now I'd like to do the same from C# code (running on .NET 6) that I'm writing, that should end up being an Azure Function in the end - so it's a "server-to-server", behind-the-scenes, no interactive login context kind of scenario.
I can connect to Dataverse via the TDS endpoint using this connection string - as long as I'm running the app interactively - as me, in my user context:
Server=myorg.crm4.dynamics.com,5558;Authentication=Active Directory Password;Database=my_dbname;User Id=my_email;Password=my_pwd;
However - this won't work with a server-to-server "daemon"-style setup.
Since I'm using .NET 6 (for the Azure Function), and since I want to run some custom SQL statements, I cannot use the "CRM XRM Client" tooling (with the IOrganizationService classes) - I need to use straight ADO.NET - any idea would I could define an ADO.NET compatible connection string, that would use a Client ID and Client Secret (which I both have at my disposal)?
I've tried a great many values for the Authentication=...... setting - but none have worked so far. Any place I can find a complete list of the supported values for this connection string parameter?
Thanks for any help or pointers!

SQLIte from .net framework

My question is how to get SQLite working under .NET framework, v 4.7+, period and where the location of the DB changes with each customer site I install at?
Issue:
My problem with SQLite in .NET framework is I keep getting DLL for SQLite not found or DB factory missing. After spending 3-4 days perusing the i-net and trying different things suggested I've not found a resolve. Maybe I'm that 5-pound hammer is a glass factory 8).
Here's the background:
Requirement is Caller/Host is VBA application, the called "dll" must adhere to COM interface functionality.
I've built a .NET COM exposed DLL that accesses data from a DB using .NET framework 4.7.2, EF 6.4.4 and DBMS SQL Express 2019 which works fine. Challenge is customer IT depts acceptance of solution.
There are 2 DBs, one DB is a read many, write none with 7 tables, one table has 950,000+ rows. The other DB has 2 tables both read many, write occasionally. Both DBs are shared by multiple workstations.
The goal is to replace SQL Express with SQLite. Reason: SQLite is self-contained with no IT involvement to install, except to grant install rights in the first place 8).
Tried XML/Json source file with memory dataset, performance was slow on load, 10-30 seconds, and ran into "out of memory" errors testing with minimal PC spec I find customers using.
Found I can do the data access using .NET 5 or 6 with EF core. I can pass the location of the DB file(s) as required (the DB will be in a folder local or even on a server file share different than my DLL). This took less than a day to get working. The rub here is that .NET framework cannot call .NET DLLs in my testing.
In .NET 6 I call the Class Library as follows:
PostalDataLib tstDB = new(sysDBFQN, custDBFQN);
The Constructor looks like this:
public PostalDataLib(string sysDbFQN, string cusDbFQN)
{
if (!File.Exists(sysDbFQN)) throw new ArgumentException("Invalid FQN, file not found", nameof(sysDbFQN));
if (!File.Exists(cusDbFQN)) throw new ArgumentException("Invalid FQN, file not found", nameof(cusDbFQN));
_custFQN = cusDbFQN;
_sysFQN = sysDbFQN;
_optsPostalData = new DbContextOptionsBuilder<PostalDB>().UseSqlite($"Data Source={sysDbFQN}").Options;
_optsCustData = new DbContextOptionsBuilder<PostalCustDB>().UseSqlite($"Data Source={cusDbFQN}").Options;
}
I then open the DB(s) as follows:
using PostalDB dbctx = new(_optsPostalData);
using PostalCustDB dbctxcus = new(_optsCustData);
But I can't seem to make this workable under .NET framework 4.7.2.
As an update to this.
I've moved the Data access layer into two parts. A tcp Server and the main VBA facing DLL as a TCP client using synchronous methods. This allows for 1 Data access point (added benefit) from multiple PCs. The server side is written using .Net 6.0. The client is written using .Net framework 4.7.2. Using Json payloads as messaging. The TCP elements are using WatsonTCP. Had to use sources not NUget package in the Client to address newtonsoft.Json version issues. By launching the server as a windows scheduler task I get the silent running like a windows service but as I found out without the issue of not finding the appsettings.json file.
The bottom line is it seems that there is no clear information being offered on how to make SQLIte dynamically point to DB via a config file FQN in .net framework 4.7. (not the same as does not support)

Unable connect to MySQL from C# but possible from other apps

I wanted to change MySQL server in my C# app. The app is run in my company and both - old and new MySQL servers are only accessible from the intranet. The problem is I can connect from HeidiSQL software and from python code but not from C#...
I have tried all possible solutions what I found, for ex. disabling firewall, using different packages from NuGet, modifying connection string, I created new console project only to paste various MySQL connection code - always with same error
Message: Unable to connect to any of the specified MySQL hosts.
Source: MySql.Data
Number: 1042
I', using .net Framework 4.5.2 (4.6.1 in my test project) and MySQL Server is '5.6.44-log - MySQL Community Server (GPL)'
One of C# connection code example that I have tested that is NOT working
using MySql.Data.MySqlClient;
public static MySqlConnection DB_connection;
DB_connection = new MySqlConnection(#"Server=MyIP;Database=myDB;Uid=my_user;Pwd=pass;");
try
{
DB_connection.Open();
isConn = true;
}
catch (...)
Working python code run from the same PC
import pymysql
import pprint
connection = pymysql.connect(host='MyIP',
user='my_user',
password='pass',
db='myDB')
try:
with connection.cursor() as cursor:
sql = "select * from table;"
cursor.execute(sql)
# connection.commit()
result = cursor.fetchall()
pprint.pprint(result)
finally:
connection.close()
Don't see anything wrong with your posted connection string but in case your's is a replicated scenario (I mean DB replication exists) then you will have to specify the replicated server IP/hostname as well like
Server=serverAddress1, serverAddress2, serverAddress3;Database=myDataBase;
Uid=myUsername;Pwd=myPassword;

MongoDB server State is disconnected

I am using MongoDb server installed on VM Ubuntu 14 on Azure, and I use this
tutroial, with last version. I add the port of mongo 27017 too.
And I connect to it directly and add Database with some collections.
I use the mongoDb .Net Driver on VS2015 in C# with version 2.0.1 (using link)
and try to connect to the Mongo Server, but the state of the server is disconnected
var client = new MongoClient("mongodb://name.cloudapp.net:27017");
var state = client.Cluster.Description.State;
MessageBox.Show(state.ToString());
I used it before the same steps and nothing happen, just I don't know where is the problem
Try this please. I think this may work. Just have to add one line to enumerate all databases.
var client = new MongoClient("mongodb://name.cloudapp.net:27017");
var databases = client.ListDatabases();
var state = client.Cluster.Description.State;
MessageBox.Show(state.ToString());
This answer explains better.

Mongodb connection string Invalid keyword data source

I am using C# official MongoDB nugget library.
I hosted the web service on Windows Azure, and the MongoDB database at Mongolab.
I connected to a server with the code below:
connection = mongodb://user:pass#ds049999.mongolab.com:45077"
_server = MongoServer.Create(connection);
Previously it is working fine and it work fine at local testing server, but at my latest publish I get error below:
ExceptionMessage":"Invalid keyword 'data source'."," ....... at MongoDB.Driver.MongoConnectionStringBuilder.set_Item(String keyword, Object value)
Anyone know what is the problem?
The following works for me:
var client = new MongoClient("mongodb://user:pass#ds049999.mongolab.com:45077");
var server = client.GetServer();
var database = server.GetDatabase("MyDataBaseName");
If your username or password contains special characters, you might want to encode them.
Please also note that your database username and password may well be different to your MongoLab login!

Categories