/// <summary> /// Creates a new instance of the ServiceBusNotificationService class /// </summary> public ServiceBusNotificationService(IOptions <NotificationServiceOptions> options, ILogger <ServiceBusNotificationService> logger) { if (options?.Value == null) { throw new ArgumentNullException(nameof(options), "No configuration is defined for the notification service in the appsettings.json."); } if (options.Value.ServiceBus == null) { throw new ArgumentNullException(nameof(options), "No ServiceBus element is defined in the configuration for the notification service in the appsettings.json."); } if (string.IsNullOrWhiteSpace(options.Value.ServiceBus.ConnectionString)) { throw new ArgumentNullException(nameof(options), "No connection string is defined in the configuration of the Service Bus notification service in the appsettings.json."); } if (string.IsNullOrWhiteSpace(options.Value.ServiceBus.QueueName)) { throw new ArgumentNullException(nameof(options), "No queue name is defined in the configuration of the Service Bus notification service in the appsettings.json."); } _options = options.Value; _logger = logger; _queueClient = new QueueClient(_options.ServiceBus.ConnectionString, _options.ServiceBus.QueueName); _logger.LogInformation(LoggingEvents.Configuration, "ConnectionString = {connectionstring}", _options.ServiceBus.ConnectionString); _logger.LogInformation(LoggingEvents.Configuration, "QueueName = {queuename}", _options.ServiceBus.QueueName); }
public static void AddNotificationService <TRecipient, TNotificationRepository>(this IServiceCollection serviceCollection, Action <NotificationServiceOptions> optionsBuilder = null, params Func <IServiceProvider, INotificationTransporter <TRecipient> >[] transporterFactories) where TNotificationRepository : class, INotificationRepository { serviceCollection.AddScoped <INotificationRepository, TNotificationRepository>(); serviceCollection.AddScoped <INotificationService <TRecipient> >(factory => { var serviceOptions = new NotificationServiceOptions(); if (optionsBuilder != null) { optionsBuilder(serviceOptions); } var manager = new NotificationService <TRecipient>(factory.GetService <INotificationRepository>(), factory.GetService <INotificationTransportManager <TRecipient> >(), serviceOptions); return(manager); }); serviceCollection.AddScoped <INotificationTransportManager <TRecipient> >(factory => { var manager = new NotificationTransportManager <TRecipient>(); foreach (var transporterFactory in transporterFactories) { manager.Register(transporterFactory(factory)); } return(manager); }); }
public MockNotificationService() { Options = new NotificationServiceOptions(); MockNotificationRepository = new Mock <INotificationRepository>(); MockNotificationTransportManager = new Mock <INotificationTransportManager <Recipient> >(); MockNotificationTransporter = new Mock <INotificationTransporter <Recipient> >(); MockNotificationRecipientResolver = new Mock <INotificationRecipientResolver <Recipient> >(); MockNotificationTransporter.Setup(x => x.RecipientResolver).Returns(MockNotificationRecipientResolver.Object); MockNotificationTransporter.Setup(x => x.TransporterType).Returns("email"); // Our pending notifications should have a minimum timestamp in relation to our options. ExpectedPendingNotificationsTimestampCheck = (value) => value <= DateTime.UtcNow.AddSeconds(Options.PendingNotificationOffsetInSeconds * -1) && value >= DateTime.UtcNow.AddSeconds((Options.PendingNotificationOffsetInSeconds + 5) * -1); Service = new NotificationService <Recipient>(MockNotificationRepository.Object, MockNotificationTransportManager.Object, Options); }
/// <summary> /// Creates a new instance of the ServiceBusNotificationService class /// </summary> public ServiceBusNotificationService(IConfiguration configuration, IOptions <NotificationServiceOptions> options, ILogger <ServiceBusNotificationService> logger) { try { this.configuration = configuration; if (options?.Value == null) { throw new ArgumentNullException(nameof(options), "No configuration is defined for the notification service in the appsettings.json."); } if (options.Value.ServiceBus == null) { throw new ArgumentNullException(nameof(options), "No ServiceBus element is defined in the configuration for the notification service in the appsettings.json."); } if (string.IsNullOrWhiteSpace(options.Value.ServiceBus.ConnectionString)) { throw new ArgumentNullException(nameof(options), "No connection string is defined in the configuration of the Service Bus notification service in the appsettings.json."); } if (string.IsNullOrWhiteSpace(options.Value.ServiceBus.QueueName)) { throw new ArgumentNullException(nameof(options), "No queue name is defined in the configuration of the Service Bus notification service in the appsettings.json."); } this.options = options.Value; this.logger = logger; if (this.options.ServiceBus.ConnectionString.ToLower().StartsWith("endpoint=")) { logger.LogInformation("Using Service Bus connectionString to connect to the Service Bus namespace..."); queueClient = new QueueClient(this.options.ServiceBus.ConnectionString, this.options.ServiceBus.QueueName); } else if (this.options.ServiceBus.ConnectionString.ToLower().EndsWith(".servicebus.windows.net")) { logger.LogInformation("Using System-Assigned Managed Instance to connect to the Service Bus namespace..."); var tokenProvider = new ManagedServiceIdentityTokenProvider(); queueClient = new QueueClient(this.options.ServiceBus.ConnectionString, this.options.ServiceBus.QueueName, tokenProvider); } else { logger.LogError("The Service Bus connectionString format is wrong", this.options.ServiceBus.ConnectionString); } logger.LogInformation("QueueClient successfully created.ConnectionString = [{connectionstring}] QueueName = [{queuename}]", this.options.ServiceBus.ConnectionString, this.options.ServiceBus.QueueName); var instrumentationKey = configuration["ApplicationInsights:InstrumentationKey"]; if (string.IsNullOrWhiteSpace(instrumentationKey)) { return; } telemetryClient = new TelemetryClient { InstrumentationKey = instrumentationKey }; } catch (Exception ex) { logger.LogError(ex, "An error occurred while creating an instance of the ServiceBusNotificationService class"); throw; } }