MongoDB server State is disconnected - c#

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.

Related

MongoConfigurationException when SRV connection string

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.

Azure - Programmatically add new MySQL db instance

I am trying to programmatically add new MySQL db instance to azure portal.
I looked at this library: https://github.com/Azure/azure-libraries-for-net but i only see an example of how to create a SQL Server and not a MySQL.
var credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithDefaultSubscription();
var sqlServer = azure.SqlServers.Define(sqlServerName)
.WithRegion(Region.USEast)
.WithNewResourceGroup(rgName)
.WithAdministratorLogin(AdministratorLogin)
.WithAdministratorPassword(AdministratorPassword)
.WithNewFirewallRule(FirewallRuleIPAddress)
.WithNewFirewallRule(FirewallRuleStartIPAddress, FirewallRuleEndIPAddress)
.Create();
var database = sqlServer.Databases
.Define(DatabaseName)
.Create();
Any idea if its supported to programmatically create a MySQL server as well?
Looking at the release notes and SDK code, it seems the ability to manage MySQL databases is still not supported (as of version 1.3).
What you could do is consume the REST API for managing Azure MySQL databses directly. For creation of database, please look at Create Or Update Database operation.
To get started with Azure REST API, you may find this link useful: https://learn.microsoft.com/en-us/rest/api/.

MS Sync Framework and SQL Server Compact

I develop a Windows C# application which can work in Online and Offline mode.
When in Online mode it connects to a SQL Server. In Offline mode it connects to a local DB.
I use the Microsoft Sync Framework 2.1 to sync the 2 databases on demand.
Until now I used a LocalDB instance of SQL Server as the local database. But it is a pain to setup the system automatically during the installation process of my application. So I tought to use SQL Server Compact 3.5 or 4.0 which is very easy to distribute (comes in a single file).
But I cannot get it to even compile the provisioning code of the Compact DB:
DbSyncScopeDescription scopeDesc = new DbSyncScopeDescription("MyScope");
SqlCeConnection clientConn = new SqlCeConnection(OfflineConnectionString);
var clientProvision = new SqlCeSyncScopeProvisioning(clientConn, scopeDesc);
clientProvision.Apply();
which I used before (without the Ce classes) but SqlCeSyncScopeProvisioning cannot be resolved.
Something is terribly wrong here.
How can I sync my CompactDB to distribute this as my local database?
First ensure, you have successfully installed the sdk?
After this, make sure you have added the following references:
Microsoft.Synchronization.Data.dll,
Microsoft.Synchronization.Data.Server.dll
Microsoft.Synchronization.Data.SqlServer.dll
Microsoft.Synchronization.Data.SqlServerCe.dll
Also in my case it worked with adding
System.Data.SqlServerCe - 4.0
EDIT
Against your comment, this is only working if you use SQL Server CE 4.
I've tried it now with SQL Server CE 3.5 and indeed I could reproduce your issue.
Switching to SQL Server CE 4.0 fixed it.
ExampleTable 4.0
TestCode
var scopeDesc = new DbSyncScopeDescription("MyScope");
var tbl = new DbSyncTableDescription("TestTable");
var pkColumn = new DbSyncColumnDescription("Id", "int");
pkColumn.IsPrimaryKey = true;
tbl.Columns.Add(pkColumn);
tbl.Columns.Add(new DbSyncColumnDescription("Name", "nvarchar(254)"));
scopeDesc.Tables.Add(tbl);
var clientConn = new SqlCeConnection(#"Data Source=test.sdf;Persist Security Info=False;");
var clientProvision = new SqlCeSyncScopeProvisioning(clientConn, scopeDesc);
clientProvision.Apply();
Result
Everything compiles nicely. After following the above steps, you should be able to easily migrate your code to SQL Server CE

Shutdown mongoDb server with c# driver 2.2.3

With previous version of C# drivers (1.x) I could do :
var client = new MongoClient(settings);
var server = client.GetServer();
server.Shutdown();
How can I do this with driver version 2.2.3 ?
Update
Well the best I could find is something like this :
try
{
var client = new MongoClient(settings);
var adminDatabase = client.GetDatabase("admin");
var cmd = new BsonDocument("shutdown", 1);
adminDatabase.RunCommand<BsonDocument>(cmd);
}
catch (MongoConnectionException e)
{
if (!(e.InnerException is EndOfStreamException))
{
throw;
}
}
but I dont really like this, the Try/catch etc ...
They told me at the Google Groups Page that is because it should never be used from most applications.
Craig Wilson mentioned that shutdown is simply a command that can be send using
db.RunCommand("{shutdown: 1}")
So it isn't anymore available in the API .net Version 2.0.0 and above.
This is the best that I could find after some intensive searching today. I am using the MongoDB C# driver 2.2. There are no special credentials to my mongod instance, it is all default settings. I would imagine this code would change a bit if there are special login credentials for the admin database.
// Connecting. 1 DB for actual usage, 1 for running the shutdown command
Client = new MongoClient("mongodb://127.0.0.1:27017");
Database = Client.GetDatabase(DBName);
AdminDatabase = Client.GetDatabase("admin");
// Shutting down the DB "cleanly"
AdminDatabase.RunCommandAsync<BsonDocument>(new JsonCommand<BsonDocument>("{shutdown: 1}"));
From what I can tell by watching the mongod instance in a command prompt my application successfully connects, writes, reads, and then shuts down the mongod instance with dbexit: rc: 0 which from what I can tell means it shutdown correctly, I faintly remember seeing dbexit: rc: 12 when shutting down that way I was before (don't even ask).

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