/// <summary>
        /// Plays the event.
        /// </summary>
        /// <param name="domainEvent">The domain event.</param>
        private void PlayEvent(RequestSentToEmailProviderEvent domainEvent)
        {
            this.Body          = domainEvent.Body;
            this.Subject       = domainEvent.Subject;
            this.IsHtml        = domainEvent.IsHtml;
            this.FromAddress   = domainEvent.FromAddress;
            this.MessageStatus = MessageStatus.InProgress;

            foreach (String domainEventToAddress in domainEvent.ToAddresses)
            {
                MessageRecipient messageRecipient = new MessageRecipient();
                messageRecipient.Create(domainEventToAddress);
                this.Recipients.Add(messageRecipient);
            }
        }
        public void RequestSentToEmailProviderEvent_CanBeCreated_IsCreated()
        {
            RequestSentToEmailProviderEvent requestSentToProviderEvent = new RequestSentToEmailProviderEvent(TestData.MessageId, TestData.FromAddress, TestData.ToAddresses, TestData.Subject,
                                                                                                             TestData.Body, TestData.IsHtmlTrue);

            requestSentToProviderEvent.ShouldNotBeNull();
            requestSentToProviderEvent.AggregateId.ShouldBe(TestData.MessageId);
            requestSentToProviderEvent.EventId.ShouldNotBe(Guid.Empty);
            requestSentToProviderEvent.MessageId.ShouldBe(TestData.MessageId);
            requestSentToProviderEvent.FromAddress.ShouldBe(TestData.FromAddress);
            requestSentToProviderEvent.ToAddresses.ShouldBe(TestData.ToAddresses);
            requestSentToProviderEvent.Subject.ShouldBe(TestData.Subject);
            requestSentToProviderEvent.Body.ShouldBe(TestData.Body);
            requestSentToProviderEvent.IsHtml.ShouldBe(TestData.IsHtmlTrue);
        }
        /// <summary>
        /// Sends the request to provider.
        /// </summary>
        /// <param name="fromAddress">From address.</param>
        /// <param name="toAddresses">To addresses.</param>
        /// <param name="subject">The subject.</param>
        /// <param name="body">The body.</param>
        /// <param name="isHtml">if set to <c>true</c> [is HTML].</param>
        public void SendRequestToProvider(String fromAddress,
                                          List <String> toAddresses,
                                          String subject,
                                          String body,
                                          Boolean isHtml)
        {
            if (this.MessageStatus != MessageStatus.NotSet)
            {
                throw new InvalidOperationException("Cannot send a message to provider that has already been sent");
            }

            RequestSentToEmailProviderEvent requestSentToProviderEvent = new RequestSentToEmailProviderEvent(this.AggregateId, fromAddress, toAddresses, subject, body, isHtml);

            this.ApplyAndAppend(requestSentToProviderEvent);
        }
        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            Console.Title = "Messaging Service";

            //At this stage, we only need our hosting file for ip and ports
            IConfigurationRoot config = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
                                        .AddJsonFile("hosting.json", optional: true)
                                        .AddJsonFile("hosting.development.json", optional: true)
                                        .AddEnvironmentVariables().Build();

            IHostBuilder hostBuilder = Host.CreateDefaultBuilder(args);

            hostBuilder.ConfigureLogging(logging =>
            {
                logging.AddConsole();
            }).ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup <Startup>();
                webBuilder.UseConfiguration(config);
                webBuilder.UseKestrel();
            })
            .ConfigureServices(services =>
            {
                RequestSentToEmailProviderEvent e = new RequestSentToEmailProviderEvent(Guid.Parse("2AA2D43B-5E24-4327-8029-1135B20F35CE"), "", new List <String>(),
                                                                                        "", "", true);

                RequestSentToSMSProviderEvent s = new RequestSentToSMSProviderEvent(Guid.NewGuid(), "", "", "");

                TypeProvider.LoadDomainEventsTypeDynamically();

                services.AddHostedService <SubscriptionWorker>(provider =>
                {
                    IDomainEventHandlerResolver r =
                        provider.GetRequiredService <IDomainEventHandlerResolver>();
                    EventStorePersistentSubscriptionsClient p = provider.GetRequiredService <EventStorePersistentSubscriptionsClient>();
                    HttpClient h = provider.GetRequiredService <HttpClient>();
                    SubscriptionWorker worker = new SubscriptionWorker(r, p, h);
                    worker.TraceGenerated    += Worker_TraceGenerated;
                    return(worker);
                });
            });
            return(hostBuilder);
        }
Beispiel #5
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            ConfigurationReader.Initialise(Startup.Configuration);

            Startup.ConfigureEventStoreSettings();

            this.ConfigureMiddlewareServices(services);

            services.AddTransient <IMediator, Mediator>();

            Boolean useConnectionStringConfig = Boolean.Parse(ConfigurationReader.GetValue("AppSettings", "UseConnectionStringConfig"));

            if (useConnectionStringConfig)
            {
                String connectionStringConfigurationConnString = ConfigurationReader.GetConnectionString("ConnectionStringConfiguration");
                services.AddSingleton <IConnectionStringConfigurationRepository, ConnectionStringConfigurationRepository>();
                services.AddTransient <ConnectionStringConfigurationContext>(c =>
                {
                    return(new ConnectionStringConfigurationContext(connectionStringConfigurationConnString));
                });

                // TODO: Read this from a the database and set
            }
            else
            {
                services.AddEventStoreClient(Startup.ConfigureEventStoreSettings);
                services.AddEventStoreProjectionManagementClient(Startup.ConfigureEventStoreSettings);
                services.AddEventStorePersistentSubscriptionsClient(Startup.ConfigureEventStoreSettings);

                services.AddSingleton <IConnectionStringConfigurationRepository, ConfigurationReaderConnectionStringRepository>();
            }


            services.AddTransient <IEventStoreContext, EventStoreContext>();
            services.AddSingleton <IMessagingDomainService, MessagingDomainService>();
            services.AddSingleton <IAggregateRepository <EmailAggregate, DomainEventRecord.DomainEvent>, AggregateRepository <EmailAggregate, DomainEventRecord.DomainEvent> >();
            services.AddSingleton <IAggregateRepository <SMSAggregate, DomainEventRecord.DomainEvent>, AggregateRepository <SMSAggregate, DomainEventRecord.DomainEvent> >();

            RequestSentToEmailProviderEvent r = new RequestSentToEmailProviderEvent(Guid.Parse("2AA2D43B-5E24-4327-8029-1135B20F35CE"), "", new List <String>(),
                                                                                    "", "", true);

            TypeProvider.LoadDomainEventsTypeDynamically();

            this.RegisterEmailProxy(services);
            this.RegisterSMSProxy(services);

            // request & notification handlers
            services.AddTransient <ServiceFactory>(context =>
            {
                return(t => context.GetService(t));
            });

            services.AddSingleton <IRequestHandler <SendEmailRequest, String>, MessagingRequestHandler>();
            services.AddSingleton <IRequestHandler <SendSMSRequest, String>, MessagingRequestHandler>();

            services.AddSingleton <Func <String, String> >(container => (serviceName) =>
            {
                return(ConfigurationReader.GetBaseServerUri(serviceName).OriginalString);
            });
            var httpMessageHandler = new SocketsHttpHandler
            {
                SslOptions =
                {
                    RemoteCertificateValidationCallback = (sender,
                                                           certificate,
                                                           chain,
                                                           errors) => true,
                }
            };
            HttpClient httpClient = new HttpClient(httpMessageHandler);

            services.AddSingleton(httpClient);

            Dictionary <String, String[]> eventHandlersConfiguration = new Dictionary <String, String[]>();

            if (Startup.Configuration != null)
            {
                IConfigurationSection section = Startup.Configuration.GetSection("AppSettings:EventHandlerConfiguration");

                if (section != null)
                {
                    Startup.Configuration.GetSection("AppSettings:EventHandlerConfiguration").Bind(eventHandlersConfiguration);
                }
            }
            services.AddSingleton <EmailDomainEventHandler>();
            services.AddSingleton <SMSDomainEventHandler>();
            services.AddSingleton <Dictionary <String, String[]> >(eventHandlersConfiguration);

            services.AddSingleton <Func <Type, IDomainEventHandler> >(container => (type) =>
            {
                IDomainEventHandler handler = container.GetService(type) as IDomainEventHandler;
                return(handler);
            });


            services.AddSingleton <IDomainEventHandlerResolver, DomainEventHandlerResolver>();
        }