I am not able to connect remote mongodb from my asp.net application. Though I am able connect it through mongo console.It gives me following error
"An exception of type 'MongoDB.Driver.MongoConnectionException'
occurred in MongoDB.Driver.dll but was not handled in user code
Additional information: Unable to connect to server troup.mongohq.com:10035: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."
Also I am able to connect my asp.net application from my local database.
Here is my code to connect mongodb by c#
var connectionString = "mongodb://appuser:xxxxx;#troup.mongohq.com:10035/PowrOfYouDev2";
var client = new MongoClient(connectionString);
var server = client.GetServer();
var database = server.GetDatabase("PowrOfYouDev2");
var collection = database.GetCollection<Entity>("Test");
var entity = new Entity { Name = "Tom" };
collection.Insert(entity);
var id = entity.Id;
var query = Query<Entity>.EQ(x => x.Id, id);
entity = collection.FindOne(query);
string str = entity.Name;
I am inserting image of error
Please help me.
Way late... But is there a reason you had that semicolon in the path?
"mongodb://appuser:xxxxx;#troup.mongohq.com:10035/PowrOfYouDev2
Seems like it should have been:
"mongodb://appuser:xxxxx#troup.mongohq.com:10035/PowrOfYouDev2
Related
I have mongodb database with username and password, I am trying to access to it from C#, I'm worjking with MongoDBDriver version 2.9.1 from NuGet, I tried the following code:
var client = new MongoClient($"mongodb://usre1:123#localhost/GSCADB");
IMongoDatabase db = client.GetDatabase("GSCADB");
var collection = db.GetCollection<BsonDocument>("GSCALogs");
int TotalFiles = collection.Find(new BsonDocument()).ToList().Count;
but this gives the exception:
Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1.
I'm creating a web service that reads a file that contains a XMLA query and execute it against SSAS. My problem is that everything works locally, but when I publish my solution on a Windows Server 2008 R2 an exception keeps being generated.
Here is my code:
private String sendXMLARequest(String sXMLARequest, String sTargetServer, StreamWriter logger)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(sXMLARequest);
String Json = JsonConvert.SerializeXmlNode(doc);
Server server = new Server();
server.Connect(sTargetServer);
server.Execute(sXMLARequest);
XmlaResultCollection rsCollection = server.Execute(sXMLARequest);
string dis = server.Version.ToString();
String s = null;
foreach (XmlaResult res in rsCollection)
{
s += res.Value.ToString();
foreach (XmlaMessage message in res.Messages)
{
logger.WriteLine(message.ToString());
if (message is XmlaError)
Json = s + "ERROR : " + message.Description;
else
Json = s + "WARNIN : " + message.Description;
}
}
server.Disconnect();
JavaScriptSerializer js = new JavaScriptSerializer();
return js.Serialize(Json.ToString()); ;
}
EXCEPTION : Microsoft.AnalysisServices.ConnectionException: A
connection cannot be made. Ensure that the server is running. --->
System.IO.IOException: Unable to read data from the transport
connection: An existing connection was forcibly closed by the remote
host. ---> System.Net.Sockets.SocketException: An existing connection
was forcibly closed by the remote host
I was trying to connect to mongoDB database but I figured out that gerserver function is not available anymore. Here is a part of my code that achieves this.I am getting error while I am tryng to create a database because I am trying to convert it implicitly(MongoDatabase db=client.GetDatabase("test")
string connectionString = "Server=localhost:27017";
Console.WriteLine("Connection MongoDB");
MongoClient client = new MongoClient(connectionString);
//MongoServer server = client.GetServer()
MongoDatabase db = client.GetDatabase("test");
The MongoClient constructor takes a MongoDB connection URI, not a .Net style connection string.
So it should be:
MongoClient client = new MongoClient("mongodb://localhost:27017");
IMongoDatabase db = client.GetDatabase("test");
Note that I also changed the type of db to IMongoDatabase as that's what's returned by GetDatabase.
I try to connect to server and get database. It run properly but it VS2013 show me a warning :
Warning 1 'MongoDB.Driver.MongoClientExtensions.GetServer(MongoDB.Driver.MongoClient)' is obsolete: 'Use the new API instead.
string connectionString = "mongodb://localhost:27017";
MongoClientSettings settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
MongoClient mongoClient = new MongoClient(settings);
var server = mongoClient.GetServer();
var db = server.GetDatabase("bookstore");
var bookCollection = db.GetCollection<Book>("Book");
Can someone help me solve this ? Tks for reading.
The MongoServer class was deprecated in version 2.0.0 (see here). You can call GetDatabase() directly on the MongoClient object:
MongoClient mongoClient = new MongoClient(settings);
var db = mongoClient.GetDatabase("bookstore");
More documentation about connecting to the MongoDB server, retrieving a database, etc. can be found in the reference documentation.
I changed my code to following:
var mongoUrl = new MongoUrl(connectionString);
var mongoClient = new MongoClient(mongoUrl);
MongoServer server = new MongoServer(MongoServerSettings.FromClientSettings(mongoClient.Settings));
The example code from #Robby works, but it doesn't return what your code is expecting; it returns Interface objects. The C# driver was updated to utilize Interface methods, as well as a number of asynchronous functions, so updating your code is probably a good idea.
The new way to get a database is -- well, you don't get a database anymore. You get an IMongoDatabase which is an interface to the database. Also, you should not work with MongoCollections anymore (from the MongoDatabase object), when you get an IMongoDatabase you will work with an IMongoCollection. Refactoring? You bet! But it's worth it.
I would also recommend putting your default database in the Mongo URL formatted connection strings. That way you can keep hard-coded constants, such as the database name, out of your code.
// Get your connection string -- use the URL format as in example below:
// name="MongoConnectionStr" connectionString="mongodb://localhost/bookstore"
var connectionString = ConfigurationManager.ConnectionStrings["MongoConnectionStr"].ConnectionString;
var mongoUrl = MongoUrl.Create(connectionString);
var client = new MongoClient(connectionString);
// Use the Mongo URL to avoid hard-coding the database name.
var db = new MongoClient(mongoUrl).GetDatabase(mongoUrl.DatabaseName);
// books below is an IMongoCollection
var books = db.GetCollection<Book>("Books");
I am trying to insert elements into a MongoLab database (Sandbox plan) using C# (by parsing a xml file, but that is not the relevant part).
var connectionString = "mongodb://user:pass#ds011111.mongolab.com:11111/db";
var server = client.GetServer();
var database = server.GetDatabase("mydb");
var elementCollection = database.GetCollection<Entity>("entities");
XmlDocument doc = new XmlDocument();
doc.LoadXml(elementxml);
XmlNodeList elementList = doc.GetElementsByTagName("element");
foreach (XmlNode element in elementList)
{
var t = new Entity();
t.Name = element.FirstChild.InnerText;
elementCollection.Insert(t); // this causes the error below
}
This is the message I get:
WriteConcern detected an error 'not authorized for insert on mydb.entities'. (Response
was { "err" : "not authorized for insert on mydb.entities", "code" : 16544, "n" : 0,
"lastOp" : { "$timestamp" : NumberLong(0) }, "connectionId" : 33932414, "ok" : 1.0 }).
If I run the same code on localhost, everything works as intended
If I insert an element using mongo/shell I get Cannot use commands write mode, degrading to compatability mode, but it works
Does this mean I cannot populate my mongolab database with data (from C#) because I do not have the right permissions as a Sandbox user? If that is the case, what are my options?
The problem seems to be that you're authenticating to the "db" database but trying to use the "mydb" database. Except for specially-privileged/admin users, most users only have access to one database, hence the not authorized error. We run all our databases with authentication on, while the MongoDB defaults, which you're likely using locally, require no authentication; that's why you're not seeing the issue locally.
You should be grabbing the DB to be used from the URI. Here's an example from our Language Center.
// Standard URI format: mongodb://[dbuser:dbpassword#]host:port/dbname
String uri = "mongodb://user:pass#host:port/db";
MongoUrl url = new MongoUrl(uri);
MongoClient client = new MongoClient(url);
MongoServer server = client.GetServer();
MongoDatabase db = server.GetDatabase(url.DatabaseName);
If that doesn't sort you out, try our connectivity troubleshooting guide. In particular, the next thing I'd look at is whether you're using the right credentials (see the section entitled "Check your database credentials").
Finally, please don't hesitate to contact us as support#mongolab.com if you continue to have issues or have any other questions.