Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb in a portable C# app

Tags:

c#

mongodb

I am developing an app that should be portable and I am using mongodb.

By portable I means that my app has a folder with all: dlls, exes, mongo files, mongo databases. Then with this folder I can run my app in any machine.

Then I need to know:

  • Is there some library that allow me to run the mongod process when the app start and end the process when the app ends ?

  • Exists a good practice to do that stuff ?

Advices are welcome and thanks in advance.

like image 472
Eric Javier Hernandez Saura Avatar asked Nov 14 '12 18:11

Eric Javier Hernandez Saura


People also ask

Is MongoDB portable?

There isn't a "portable" MongoDB instance available. Using local storage would be a good approach if you want to keep everything contained in the JS application.

Is MongoDB good with C#?

As you already know, C# is a general-purpose language and MongoDB is a general-purpose data platform. Together, C# and MongoDB are a powerful combination.

Is there a GUI for MongoDB?

MongoDB Compass is an effective GUI tool from the makers of MongoDB. It provides a graphical view of your mongo database without you having to learn query languages. No need to write command line. It also analyses documents and displays rich structures inside this intuitive GUI.

How much RAM do I need for MongoDB?

MongoDB requires approximately 1 GB of RAM per 100.000 assets. If the system has to start swapping memory to disk, this will have a severely negative impact on performance and should be avoided.


2 Answers

According to the MongoDb installation instructions it should be quite simple.

Mongodb starts as a console application waiting for connections, so when your app starts, you should run mongodb hidden. We are always assuming that ALL the mongodb files are in place with your application files and database files are in the correct directory).

When your app terminates, you should kill the process.

Yo should set the correct paths on this example:

//starting the mongod server (when app starts)
ProcessStartInfo start = new ProcessStartInfo();     
start.FileName = dir + @"\mongod.exe";
start.WindowStyle = ProcessWindowStyle.Hidden;

start.Arguments = "--dbpath d:\test\mongodb\data";

Process mongod = Process.Start(start);

//stopping the mongod server (when app is closing)
mongod.Kill();

You can see more information about the mongod configuration and running here

like image 177
Salvador Sarpi Avatar answered Oct 05 '22 22:10

Salvador Sarpi


I needed to do the same thing and my starting point was Salvador Sarpi's answer. But, I found a couple things that needed to be added to his example.

First, you need to set UseShellExecute to false for the ProcessStartInfo object. Otherwise, you may get a security warning when the process is started asking the user if they want to run it or not. I don't think this is desired.

Second, you need to call Shutdown on the MongoServer object before killing the process. I had an issue where it locked the database and required it to be repaired if I didn't call the Shutdown method before killing the process. See Here for details on repairing

My final code is different, but for this example I used Salvador's code as the base for reference.

//starting the mongod server (when app starts)
ProcessStartInfo start = new ProcessStartInfo();     
start.FileName = dir + @"\mongod.exe";
start.WindowStyle = ProcessWindowStyle.Hidden;
// set UseShellExecute to false
start.UseShellExecute = false;

//@"" prevents need for backslashes
start.Arguments = @"--dbpath d:\test\mongodb\data";

Process mongod = Process.Start(start);

// Mongo CSharp Driver Code (see Mongo docs)
MongoClient client = new MongoClient();
MongoServer server = client.GetServer();
MongoDatabase database = server.GetDatabase("Database_Name_Here");

// Doing awesome stuff here ...

// Shutdown Server when done.
server.Shutdown();

//stopping the mongod server (when app is closing)
mongod.Kill();
like image 38
Dustin Townsend Avatar answered Oct 05 '22 21:10

Dustin Townsend