Exemple #1
0
        /// <inheritdoc />
        public Task <IMessageJournal> CreateMessageJournal(IConfiguration configuration)
        {
            var connectionName = configuration?["connectionName"];

            if (string.IsNullOrWhiteSpace(connectionName))
            {
                throw new ConfigurationErrorsException(
                          "Attribute 'connectionName' is required for MongoDB message journal");
            }

            var connectionStringSettings = ConfigurationManager.ConnectionStrings[connectionName];

            if (connectionStringSettings == null)
            {
                throw new ConfigurationErrorsException("Connection string settings \"" + connectionName + "\" not found");
            }

            var databaseName   = configuration?["database"];
            var collectionName = configuration?["collection"];
            var database       = MongoDBHelper.Connect(connectionStringSettings, databaseName);
            var journalOptions = new MongoDBMessageJournalOptions(database)
            {
                CollectionName = collectionName
            };
            var sqlMessageJournalingService = new MongoDBMessageJournal(journalOptions);

            return(Task.FromResult <IMessageJournal>(sqlMessageJournalingService));
        }
        /// <summary>
        /// Initializes a new <see cref="MongoDBMessageJournal"/> with the specified
        /// <paramref name="options"/>
        /// </summary>
        /// <param name="options">The options for this service</param>
        public MongoDBMessageJournal(MongoDBMessageJournalOptions options)
        {
            _diagnosticService = options.DiagnosticService ?? DiagnosticService.DefaultInstance;
            var myCollectionName = string.IsNullOrWhiteSpace(options.CollectionName)
                ? DefaultCollectionName
                : options.CollectionName;

            var database = options.Database;

            _messageJournalEntries = database.GetCollection <MessageJournalEntryDocument>(
                myCollectionName,
                new MongoCollectionSettings
            {
                AssignIdOnInsert = false
            });

            var minServerVersion = database.Client.Cluster.Description.Servers.Min(s => s.Version);

            _collationSupported = Feature.Collation.IsSupported(minServerVersion);
            if (_collationSupported)
            {
                var locale = string.IsNullOrWhiteSpace(options.Locale)
                    ? "en"
                    : options.Locale.Trim().ToLower();

                if ("simple".Equals(locale))
                {
                    _collation = Collation.Simple;
                }
                else if (options.CaseSensitive)
                {
                    _collation = new Collation(locale, true, strength: CollationStrength.Secondary);
                }
                else
                {
                    _collation = new Collation(locale);
                }
            }

            CreateIndexes();
        }