public static void LoadTypes() { VoucherIssuedEvent v = new VoucherIssuedEvent(Guid.NewGuid(), Guid.NewGuid(), DateTime.Now, "", ""); TransactionHasStartedEvent t = new TransactionHasStartedEvent(Guid.Parse("2AA2D43B-5E24-4327-8029-1135B20F35CE"), Guid.NewGuid(), Guid.NewGuid(), DateTime.Now, "", "", "", "", null); ReconciliationHasStartedEvent r = new ReconciliationHasStartedEvent(Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), DateTime.Now); EstateCreatedEvent e = new EstateCreatedEvent(Guid.NewGuid(), ""); MerchantCreatedEvent m = new MerchantCreatedEvent(Guid.NewGuid(), Guid.NewGuid(), "", DateTime.Now); ContractCreatedEvent c = new ContractCreatedEvent(Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), ""); MerchantBalanceChangedEvent mb = new MerchantBalanceChangedEvent(Guid.NewGuid(), Guid.NewGuid(), DateTime.Now, Guid.NewGuid(), Guid.NewGuid(), 0, 0, 0, ""); ImportLogCreatedEvent i = new ImportLogCreatedEvent(Guid.NewGuid(), Guid.NewGuid(), DateTime.MinValue); FileCreatedEvent f = new FileCreatedEvent(Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), String.Empty, DateTime.MinValue); SettlementCreatedForDateEvent s = new SettlementCreatedForDateEvent(Guid.NewGuid(), Guid.NewGuid(), DateTime.Now); StatementCreatedEvent ms = new StatementCreatedEvent(Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), DateTime.Now); TypeProvider.LoadDomainEventsTypeDynamically(); }
/// <summary> /// This method is called when the <see cref="T:Microsoft.Extensions.Hosting.IHostedService" /> starts. The implementation should return a task that represents /// the lifetime of the long running operation(s) being performed. /// </summary> /// <param name="stoppingToken">Triggered when <see cref="M:Microsoft.Extensions.Hosting.IHostedService.StopAsync(System.Threading.CancellationToken)" /> is called.</param> /// <returns> /// A <see cref="T:System.Threading.Tasks.Task" /> that represents the long running operations. /// </returns> protected override async Task ExecuteAsync(CancellationToken stoppingToken) { TypeProvider.LoadDomainEventsTypeDynamically(); foreach (KeyValuePair <Type, String> type in TypeMap.Map) { this.LogInformation($"Type name {type.Value} mapped to {type.Key.Name}"); } while (stoppingToken.IsCancellationRequested == false) { try { List <Task> fileProcessingTasks = new List <Task>(); List <FileProfile> fileProfiles = await this.FileProcessorManager.GetAllFileProfiles(stoppingToken); foreach (FileProfile fileProfile in fileProfiles) { this.LogInformation($"About to look in {fileProfile.ListeningDirectory} for files"); IDirectoryInfo listeningDirectory = this.FileSystem.DirectoryInfo.FromDirectoryName(fileProfile.ListeningDirectory); List <IFileInfo> files = listeningDirectory.GetFiles().OrderBy(f => f.CreationTime).Take(1).ToList(); // Only process 1 file per file profile concurrently, foreach (IFileInfo fileInfo in files) { this.LogInformation($"File {fileInfo.Name} detected"); String inProgressFolder = $"{fileProfile.ListeningDirectory}/inprogress/"; if (this.FileSystem.Directory.Exists(inProgressFolder) == false) { throw new DirectoryNotFoundException($"Directory {inProgressFolder} not found"); } String inProgressFilePath = $"{fileProfile.ListeningDirectory}/inprogress/{fileInfo.Name}"; fileInfo.MoveTo(inProgressFilePath, true); IRequest request = this.CreateProcessFileRequest(fileProfile, inProgressFilePath); fileProcessingTasks.Add(this.Mediator.Send(request)); } } await Task.WhenAll(fileProcessingTasks.ToArray()); } catch (Exception e) { this.LogCritical(e); } String fileProfilePollingWindowInSeconds = ConfigurationReader.GetValue("AppSettings", "FileProfilePollingWindowInSeconds"); if (string.IsNullOrEmpty(fileProfilePollingWindowInSeconds)) { fileProfilePollingWindowInSeconds = "60"; } // Delay for configured seconds before polling for files again await Task.Delay(TimeSpan.FromSeconds(int.Parse(fileProfilePollingWindowInSeconds)), stoppingToken); } }
public static void LoadTypes() { FileAddedToImportLogEvent fileAddedToImportLogEvent = new FileAddedToImportLogEvent(Guid.Empty, Guid.Empty, Guid.Empty, Guid.Empty, Guid.Empty, Guid.Empty, String.Empty, String.Empty, new DateTime()); FileLineAddedEvent fileLineAddedEvent = new FileLineAddedEvent(Guid.Empty, Guid.Empty, 0, String.Empty); TypeProvider.LoadDomainEventsTypeDynamically(); }
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); }
// 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>(); }
public static void LoadTypes() { VoucherIssuedEvent i = new VoucherIssuedEvent(Guid.NewGuid(), Guid.NewGuid(), DateTime.Now, "", ""); TypeProvider.LoadDomainEventsTypeDynamically(); }
// This method gets called by the runtime. Use this method to add services to the container. /// <summary> /// Configures the services. /// </summary> /// <param name="services">The services.</param> public void ConfigureServices(IServiceCollection services) { ConfigurationReader.Initialise(Startup.Configuration); Startup.ConfigureEventStoreSettings(); this.ConfigureMiddlewareServices(services); services.AddTransient <IMediator, Mediator>(); services.AddSingleton <IEstateManagementManager, EstateManagementManager>(); 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.AddEventStoreProjectionManagerClient(Startup.ConfigureEventStoreSettings); services.AddEventStorePersistentSubscriptionsClient(Startup.ConfigureEventStoreSettings); services.AddSingleton <IConnectionStringConfigurationRepository, ConfigurationReaderConnectionStringRepository>(); } services.AddTransient <IEventStoreContext, EventStoreContext>(); services.AddSingleton <IEstateManagementRepository, EstateManagementRepository>(); services.AddSingleton <IDbContextFactory <EstateReportingContext>, DbContextFactory <EstateReportingContext> >(); Dictionary <String, String[]> handlerEventTypesToSilentlyHandle = new Dictionary <String, String[]>(); if (Startup.Configuration != null) { IConfigurationSection section = Startup.Configuration.GetSection("AppSettings:HandlerEventTypesToSilentlyHandle"); if (section != null) { Startup.Configuration.GetSection("AppSettings:HandlerEventTypesToSilentlyHandle").Bind(handlerEventTypesToSilentlyHandle); } } services.AddSingleton <Func <String, EstateReportingContext> >(cont => (connectionString) => { return(new EstateReportingContext(connectionString)); }); DomainEventTypesToSilentlyHandle eventTypesToSilentlyHandle = new DomainEventTypesToSilentlyHandle(handlerEventTypesToSilentlyHandle); services.AddTransient <IEventStoreContext, EventStoreContext>(); services.AddSingleton <IAggregateRepository <EstateAggregate.EstateAggregate, DomainEventRecord.DomainEvent>, AggregateRepository <EstateAggregate.EstateAggregate, DomainEventRecord.DomainEvent> >(); services.AddSingleton <IAggregateRepository <MerchantAggregate.MerchantAggregate, DomainEventRecord.DomainEvent>, AggregateRepository <MerchantAggregate.MerchantAggregate, DomainEventRecord.DomainEvent> >(); services.AddSingleton <IAggregateRepository <ContractAggregate.ContractAggregate, DomainEventRecord.DomainEvent>, AggregateRepository <ContractAggregate.ContractAggregate, DomainEventRecord.DomainEvent> >(); services.AddSingleton <IEstateDomainService, EstateDomainService>(); services.AddSingleton <IMerchantDomainService, MerchantDomainService>(); services.AddSingleton <IContractDomainService, ContractDomainService>(); services.AddSingleton <IModelFactory, ModelFactory>(); services.AddSingleton <Factories.IModelFactory, Factories.ModelFactory>(); services.AddSingleton <ISecurityServiceClient, SecurityServiceClient>(); ContractCreatedEvent c = new ContractCreatedEvent(Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), ""); MerchantCreatedEvent m = new MerchantCreatedEvent(Guid.NewGuid(), Guid.NewGuid(), "", DateTime.Now); EstateCreatedEvent e = new EstateCreatedEvent(Guid.NewGuid(), ""); TypeProvider.LoadDomainEventsTypeDynamically(); // request & notification handlers services.AddTransient <ServiceFactory>(context => { return(t => context.GetService(t)); }); services.AddSingleton <IRequestHandler <CreateEstateRequest, String>, EstateRequestHandler>(); services.AddSingleton <IRequestHandler <CreateEstateUserRequest, Guid>, EstateRequestHandler>(); services.AddSingleton <IRequestHandler <AddOperatorToEstateRequest, String>, EstateRequestHandler>(); services.AddSingleton <IRequestHandler <CreateMerchantRequest, String>, MerchantRequestHandler>(); services.AddSingleton <IRequestHandler <AssignOperatorToMerchantRequest, String>, MerchantRequestHandler>(); services.AddSingleton <IRequestHandler <CreateMerchantUserRequest, Guid>, MerchantRequestHandler>(); services.AddSingleton <IRequestHandler <AddMerchantDeviceRequest, String>, MerchantRequestHandler>(); services.AddSingleton <IRequestHandler <MakeMerchantDepositRequest, Guid>, MerchantRequestHandler>(); services.AddSingleton <IRequestHandler <CreateContractRequest, String>, ContractRequestHandler>(); services.AddSingleton <IRequestHandler <AddProductToContractRequest, String>, ContractRequestHandler>(); services.AddSingleton <IRequestHandler <AddTransactionFeeForProductToContractRequest, String>, ContractRequestHandler>(); services.AddSingleton <Func <String, String> >(container => (serviceName) => { return(ConfigurationReader.GetBaseServerUri(serviceName).OriginalString); }); HttpClientHandler httpClientHandler = new HttpClientHandler { ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => { return(true); } }; HttpClient httpClient = new HttpClient(httpClientHandler); services.AddSingleton <HttpClient>(httpClient); }
/// <summary> /// This method is called when the <see cref="T:Microsoft.Extensions.Hosting.IHostedService" /> starts. The implementation should return a task that represents /// the lifetime of the long running operation(s) being performed. /// </summary> /// <param name="stoppingToken">Triggered when <see cref="M:Microsoft.Extensions.Hosting.IHostedService.StopAsync(System.Threading.CancellationToken)" /> is called.</param> /// <returns> /// A <see cref="T:System.Threading.Tasks.Task" /> that represents the long running operations. /// </returns> protected override async Task ExecuteAsync(CancellationToken stoppingToken) { Boolean useInternalSubscriptionService = Boolean.Parse(ConfigurationReader.GetValue("UseInternalSubscriptionService")); String inflightMessageCount = ConfigurationReader.GetValue("InflightMessages"); if (useInternalSubscriptionService == false) { return; } TypeProvider.LoadDomainEventsTypeDynamically(); foreach (KeyValuePair <Type, String> type in TypeMap.Map) { this.LogInformation($"Type name {type.Value} mapped to {type.Key.Name}"); } while (stoppingToken.IsCancellationRequested == false) { try { String[] subscriptionFilters = null; String groupFilter = ConfigurationReader.GetValue("SubscriptionFilter"); if (groupFilter.Contains(",")) { // Multiple filters subscriptionFilters = groupFilter.Split(","); } else { subscriptionFilters = new[] { groupFilter }; } List <SubscriptionInformation> list = await this.GetSubscriptionsList(stoppingToken); var result = list .Where(p => this.CurrentSubscriptions .All(p2 => $"{p2.ToString()}" != $"{p.EventStreamId}-{p.GroupName}")).ToList(); result = result.Where(r => subscriptionFilters.Contains(r.GroupName)).ToList(); this.LogInformation($"{result.Count} subscriptions retrieved for Group Filter [{groupFilter}]"); foreach (var subscriptionDto in result) { this.LogInformation($"Creating subscription {subscriptionDto.EventStreamId}-{subscriptionDto.GroupName}"); PersistentSubscriptionDetails persistentSubscriptionDetails = new(subscriptionDto.EventStreamId, subscriptionDto.GroupName, String.IsNullOrEmpty(inflightMessageCount) ? 0 : Int32.Parse(inflightMessageCount)); PersistentSubscription subscription = PersistentSubscription.Create(this.PersistentSubscriptionsClient, persistentSubscriptionDetails, this.DomainEventHandlerResolver); await subscription.ConnectToSubscription(); this.LogInformation($"Created subscription {subscriptionDto.EventStreamId}-{subscriptionDto.GroupName}"); this.CurrentSubscriptions.Add(subscription); } Int32 removedCount = this.CurrentSubscriptions.RemoveAll(p => p.Connected == false); if (removedCount > 0) { this.LogWarning($"Removed {removedCount} subscriptions because SubscriptionDropped."); continue; } } catch (Exception e) { this.LogCritical(e); } String persistentSubscriptionPollingInSeconds = ConfigurationReader.GetValue("EventStoreSettings", "PersistentSubscriptionPollingInSeconds"); if (String.IsNullOrEmpty(persistentSubscriptionPollingInSeconds)) { persistentSubscriptionPollingInSeconds = "60"; } // Delay for configured seconds before polling the eventstore again await Task.Delay(TimeSpan.FromSeconds(Int32.Parse(persistentSubscriptionPollingInSeconds)), stoppingToken); } }