示例#1
0
        // Inject background service, for receiving message
        public void ConfigureServices(IServiceCollection services)
        {
            var serviceProvider  = services.BuildServiceProvider();
            var loggerFactorySrv = serviceProvider.GetService <ILoggerFactory>();

            services.AddDbContextPool <EventSourcingDbContext>(options => options.UseSqlServer(
                                                                   _systemLocalConfiguration.EventDbConnection,
                                                                   //enable connection resilience
                                                                   connectOptions =>
            {
                connectOptions.EnableRetryOnFailure();
                connectOptions.CommandTimeout(Identifiers.TimeoutInSec);
            })    //.UseLoggerFactory(loggerFactorySrv)// to log queries
                                                               );
            /// Injecting message receiver background service
            ///
            #region worker background services

            #region event sourcing worker

            services.AddSingleton <IHostedService, RabbitMQSubscriberWorker>(srv =>
            {
                //get pingServicek
                var eventSourcingSrv = new EventSourcingLedger(loggerFactorySrv, srv.GetService <EventSourcingDbContext>());

                return(new RabbitMQSubscriberWorker
                           (serviceProvider, loggerFactorySrv, new RabbitMQConfiguration
                {
                    hostName = _systemLocalConfiguration.MessagesMiddleware,
                    exchange = _systemLocalConfiguration.MiddlewareExchange,
                    userName = _systemLocalConfiguration.MessagesMiddlewareUsername,
                    password = _systemLocalConfiguration.MessagesMiddlewarePassword,
                    routes = _systemLocalConfiguration.MessageSubscriberRoute?.Split('-') ?? new string[0]
                }
                           , (pingMessageCallback) =>
                {
                    try
                    {
                        var message = pingMessageCallback();
                        if (message != null)
                        {
                            var domainModel = Utilities.JsonBinaryDeserialize <DomainModels.Types.DomainModel <object> >(message);
                            var dbModel = DbModelFactory.Create(domainModel);
                            if (dbModel != null)
                            {
                                eventSourcingSrv.Add(dbModel).Wait();
                            }
                        }
                        Logger.LogInformation($"[x] Event sourcing service receiving a message from exchange: {_systemLocalConfiguration.MiddlewareExchange}, route :{_systemLocalConfiguration.MessageSubscriberRoute}");
                    }
                    catch (Exception ex)
                    {
                        Logger.LogCritical(ex, "Object de-serialization exception.");
                    }
                }));
            });

            #endregion

            #endregion
        }
 public Task <int> Add(PingModel pingEventSourcing)
 {
     return
         (_pingEventSourcingLedger
          .Add(DbModelFactory.Create(pingEventSourcing)));
 }