Example #1
0
 public MongoPersistProvider(string server, MongoDataBaseName dbName)
 {
     this.server = server;
     this.dbName = dbName;
 }
Example #2
0
        async Task <IPersist> IPeristProvider.CreatePersistAsync(IConfigure Configure, CancellationToken token)
        {
            string collectionBase = string.Empty;

            MongoPersist toReturn       = null;
            string       collectionName = string.Empty;

            bool CreateDatabase = this.dbName.DB == MongoDataBaseName.CREATE;

            //if database provided, our collection names must be unique, so we factor in applicationname
            if (!CreateDatabase)
            {
                collectionName = string.Format("{0}_{1}", Configure.ApplicationName, "messages");
            }
            else     //if we are creating database, our collectionname can be generic
            {
                this.dbName    = Configure.ApplicationName;
                collectionName = "messages";
            }



            // TODO: test if database exists and that it is not configured for another collection

            this.mongoProvider = new MongoProvider(server);

            ////if not createdatabase, then the one passed in must exist
            //if (!CreateDatabase) {
            //    if (!this.DBExists()) {
            //        throw new Exception(string.Format("{0} does not exists", this.dbName.DB));
            //    }
            //}

            bool exists = await this.CollectionExistsAsync(collectionName, token);

            if (exists)
            {
                toReturn = new MongoPersist(new MongoSettings()
                {
                    ServerName = this.server, DataBaseName = this.dbName.DB, CollectionName = collectionName
                });
                EndpointConfigPersist <EndPointConfigBase> configReader = new EndpointConfigPersist <EndPointConfigBase>(toReturn);
                //get config and ensure its the same
                EndPointConfigBase config = await configReader.GetConfigAsync(token);

                if (config.ApplicationGUID != Configure.ApplicationGUID)
                {
                    throw new Exception("Collection exists but has already been configured for another process");
                }
            }
            else     //if not, we must create

            //create collection
            {
                IMongoDatabase db = this.mongoProvider.GetClient().GetDatabase(dbName.DB);
                await db.CreateCollectionAsync(collectionName, null, token);

                IMongoCollection <PersistedMessage> collection = db.GetCollection <PersistedMessage>(collectionName);

                collection = await this.CreateIndexesAsync(collection, token);

                toReturn = new MongoPersist(new MongoSettings()
                {
                    ServerName = this.server, DataBaseName = this.dbName.DB, CollectionName = collectionName
                });
                EndpointConfigPersist <EndPointConfigBase> configReader = new EndpointConfigPersist <EndPointConfigBase>(toReturn);
                EndPointConfigBase config = new EndPointConfigBase()
                {
                    ApplicationGUID = Configure.ApplicationGUID, ApplicationName = Configure.ApplicationName, Version = "1.0"
                };
                await configReader.UpdateConfigAsync(config, token);
            }

            return(toReturn);
        }