/// <summary> /// Initializes a new <see cref="MongoDBMessageQueueingService"/> /// </summary> /// <param name="options">Options governing the behavior of the service</param> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="options"/> is <c>null</c> /// </exception> public MongoDBMessageQueueingService(MongoDBMessageQueueingOptions options) { if (options == null) { throw new ArgumentNullException(nameof(options)); } _diagnosticService = options.DiagnosticService ?? DiagnosticService.DefaultInstance; _database = options.Database; _collectionNameFactory = options.CollectionNameFactory ?? (_ => DefaultCollectionName); _securityTokenService = options.SecurityTokenService ?? new JwtSecurityTokenService(); _messageEncryptionService = options.MessageEncryptionService; }
/// <inheritdoc /> public async Task <IMessageQueueingService> CreateMessageQueueingService(QueueingElement configuration) { var connectionName = configuration.GetString("connectionName"); if (string.IsNullOrWhiteSpace(connectionName)) { throw new ConfigurationErrorsException( "Attribute 'connectionName' is required for MongoDB message queueing service"); } QueueCollectionNameFactory collectionNameFactory = null; var databaseName = configuration.GetString("database"); var collectionName = configuration.GetString("collection"); var collectionPerQueue = configuration.GetBool("collectionPerQueue") ?? false; if (!string.IsNullOrWhiteSpace(collectionName)) { collectionNameFactory = _ => collectionName; } else if (collectionPerQueue) { var collectionPrefix = (configuration.GetString("collectionPrefix") ?? "").Trim(); collectionNameFactory = queueName => (collectionPrefix + queueName).Trim(); } var connectionStringSettings = ConfigurationManager.ConnectionStrings[connectionName]; if (connectionStringSettings == null) { throw new ConfigurationErrorsException("Connection string settings \"" + connectionName + "\" not found"); } var securityTokenServiceFactory = new SecurityTokenServiceFactory(); var securitTokenConfig = configuration.SecurityTokens; var securityTokenService = await securityTokenServiceFactory.InitSecurityTokenService(securitTokenConfig); var messageEncrytionServiceFactory = new MessageEncryptionServiceFactory(); var encryptionSection = configuration.Encryption; var messageEncryptionService = await messageEncrytionServiceFactory.InitMessageEncryptionService(encryptionSection); var database = MongoDBHelper.Connect(connectionStringSettings, databaseName); var queueingOptions = new MongoDBMessageQueueingOptions(database) { CollectionNameFactory = collectionNameFactory, SecurityTokenService = securityTokenService, MessageEncryptionService = messageEncryptionService }; var messageQueueingService = new MongoDBMessageQueueingService(queueingOptions); return(messageQueueingService); }