[CSHARP-152] MongoConnectionStringBuilder fails at constructor using a connection string Created: 20/Jan/11  Updated: 02/Apr/15  Resolved: 20/Jan/11

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 0.9

Type: Bug Priority: Major - P3
Reporter: Pawel Krakowiak Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I'm trying to get a server instance by using the MongoConnectionStringBuilder to read the connection string from the application configuration file. It fails stating that the connection string is in wrong format, even for the simplest of connection strings. Example:

<add name="MongoDB" connectionString="mongodb://localhost" />

string connectionString = ConfigurationManager.ConnectionStrings["MongoDB"].ConnectionString;
var builder = new MongoConnectionStringBuilder(connectionString);

The second line will fail with the message of "System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0."

StackTrace:
at System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, ref String keyname, ref String keyvalue)
at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey)
at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules)
at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)
at MongoDB.CSharpDriver.MongoConnectionStringBuilder..ctor(String connectionString)



 Comments   
Comment by Robert Stam [ 20/Jan/11 ]

You have to decide whether to use the standard MongoDB URL connection string syntax as used by all the drivers, or the .NET format connection string syntax used only by the C# driver. When using the URL syntax use MongoUrlBuilder instead of MongoConnectionStringBuilder.

so instead of:

var builder = new MongoConnectionStringBuilder(connectionString);

use:

var builder = new MongoUrlBuilder(connectionString);

You can also pass the connection string directly to MongoServer.Create:

var server = MongoServer.Create(connectionString);

which looks for the "mongodb://" prefix to decide which format connection string you are using.

Generated at Wed Feb 07 21:35:57 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.