Exemple #1
0
        /// <summary>
        /// Sets up the MongoDB connection.
        /// </summary>
        /// <param name="functionHandler">An instance of a function handler.</param>
        public static void SetupConnection(this FunctionHandler functionHandler)
        {
            if (Database == null)
            {
                var client       = new MongoClient(string.Format("mongodb://{0}", Environment.GetEnvironmentVariable("mongo_endpoint")));
                var databameName = Environment.GetEnvironmentVariable("mongo_database_name") ?? "default_database";
                Database = client.GetDatabase(databameName);

                var collectionName = Environment.GetEnvironmentVariable("mongo_collection_name") ?? "default_collection";
                Collection = Database.GetCollection <BsonDocument>(collectionName);
            }
        }
        /// <summary>
        /// Sets up the MongoDB connection.
        /// </summary>
        public static void SetupConnection(this FunctionHandler databaseFunctionHandler)
        {
            if (Database == null)
            {
                var databaseName     = Environment.GetEnvironmentVariable("mongo_database_name") ?? "default_database";
                var databaseUser     = Environment.GetEnvironmentVariable("mongo_database_user") ?? "admin";
                var databasePassword = GetPassword();

                var mongoCredential     = MongoCredential.CreateCredential(databaseName, databaseUser, databasePassword);
                var mongoClientSettings = new MongoClientSettings
                {
                    Credential = mongoCredential,
                    Server     = new MongoServerAddress(Environment.GetEnvironmentVariable("mongo_endpoint"), 27017)
                };

                var client = new MongoClient(mongoClientSettings);
                Database = client.GetDatabase(databaseName);

                var collectionName = Environment.GetEnvironmentVariable("mongo_collection_name") ?? "default_collection";
                Collection = Database.GetCollection <BsonDocument>(collectionName);
            }
        }
 /// <summary>
 /// Gets the Mongo collection of this instance.
 /// </summary>
 /// <param name="functionHandler">This instance of a function handler.</param>
 /// <returns>An IMongoCollection<BsonDocument>.</returns>
 public static IMongoCollection <BsonDocument> GetCollection(this FunctionHandler functionHandler)
 {
     return(Collection);
 }
 /// <summary>
 /// Gets the Mongo database of this instance.
 /// </summary>
 /// <param name="functionHandler">This instance of a function handler.</param>
 /// <returns>An IMongoDatabase.</returns>
 public static IMongoDatabase GetDatabase(this FunctionHandler functionHandler)
 {
     return(Database);
 }