Beispiel #1
0
    public static void Initialize(WorkshopManagementDBContext context)
    {
        Log.Information("Ensure WorkshopManagement Database");

        Policy
        .Handle <Exception>()
        .WaitAndRetry(5, r => TimeSpan.FromSeconds(5), (ex, ts) => { Log.Error("Error connecting to DB. Retrying in 5 sec."); })
        .Execute(() => context.Database.Migrate());

        Log.Information("WorkshopManagement Database available");
    }
Beispiel #2
0
        private static IHostBuilder CreateHostBuilder(string[] args)
        {
            var hostBuilder = new HostBuilder()
                              .ConfigureHostConfiguration(configHost =>
            {
                configHost.SetBasePath(Directory.GetCurrentDirectory());
                configHost.AddJsonFile("hostsettings.json", optional: true);
                configHost.AddJsonFile($"appsettings.json", optional: false);
                configHost.AddEnvironmentVariables();
                configHost.AddEnvironmentVariables("DOTNET_");
                configHost.AddCommandLine(args);
            })
                              .ConfigureAppConfiguration((hostContext, config) =>
            {
                config.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: false);
            })
                              .ConfigureServices((hostContext, services) =>
            {
                services.AddTransient <IMessageHandler>((svc) =>
                {
                    var rabbitMQConfigSection = hostContext.Configuration.GetSection("RabbitMQ");
                    string rabbitMQHost       = rabbitMQConfigSection["Host"];
                    string rabbitMQUserName   = rabbitMQConfigSection["UserName"];
                    string rabbitMQPassword   = rabbitMQConfigSection["Password"];
                    return(new RabbitMQMessageHandler(rabbitMQHost, rabbitMQUserName, rabbitMQPassword, "Pitstop", "WorkshopManagement", ""));;
                });

                services.AddTransient <WorkshopManagementDBContext>((svc) =>
                {
                    var sqlConnectionString = hostContext.Configuration.GetConnectionString("WorkshopManagementCN");
                    var dbContextOptions    = new DbContextOptionsBuilder <WorkshopManagementDBContext>()
                                              .UseSqlServer(sqlConnectionString)
                                              .Options;
                    var dbContext = new WorkshopManagementDBContext(dbContextOptions);

                    Policy
                    .Handle <Exception>()
                    .WaitAndRetry(5, r => TimeSpan.FromSeconds(5), (ex, ts) => { Log.Error("Error connecting to DB. Retrying in 5 sec."); })
                    .Execute(() => DBInitializer.Initialize(dbContext));

                    return(dbContext);
                });

                services.AddHostedService <EventHandler>();
            })
                              .UseSerilog((hostContext, loggerConfiguration) =>
            {
                loggerConfiguration.ReadFrom.Configuration(hostContext.Configuration);
            })
                              .UseConsoleLifetime();

            return(hostBuilder);
        }
Beispiel #3
0
        /// <summary>
        /// This is the entry point of the service host process.
        /// </summary>
        private static void Main()
        {
            try
            {
                // The ServiceManifest.XML file defines one or more service type names.
                // Registering a service maps a service type name to a .NET type.
                // When Service Fabric creates an instance of this service type,
                // an instance of the class is created in this host process.

                ServiceRuntime.RegisterServiceAsync("WorkshopManagementEventHandlerType",
                                                    context => new WorkshopManagementEventHandler(context)).GetAwaiter().GetResult();

                ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(WorkshopManagementEventHandler).Name);

                // get configuration
                var    rabbitMQConfigSection = Config.GetSection("RabbitMQ");
                string host     = rabbitMQConfigSection["Host"];
                string userName = rabbitMQConfigSection["UserName"];
                string password = rabbitMQConfigSection["Password"];

                // setup messagehandler
                RabbitMQMessageHandler messageHandler = new RabbitMQMessageHandler(host, userName, password, "Pitstop", "WorkshopManagement", "");

                // setup DBContext
                var sqlConnectionString = Config.GetConnectionString("WorkshopManagementCN");
                var dbContextOptions    = new DbContextOptionsBuilder <WorkshopManagementDBContext>()
                                          .UseSqlServer(sqlConnectionString)
                                          .Options;
                var dbContext = new WorkshopManagementDBContext(dbContextOptions);

                Policy
                .Handle <Exception>()
                .WaitAndRetry(5, r => TimeSpan.FromSeconds(5), (ex, ts) => { Console.WriteLine("Error connecting to DB. Retrying in 5 sec."); })
                .Execute(() => DBInitializer.Initialize(dbContext));

                // start event-handler
                EventHandler eventHandler = new EventHandler(messageHandler, dbContext);
                eventHandler.Start();

                // Prevents this host process from terminating so services keep running.
                Thread.Sleep(Timeout.Infinite);
            }
            catch (Exception e)
            {
                ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString());
                throw;
            }
        }
Beispiel #4
0
        private static IHostBuilder CreateHostBuilder(string[] args)
        {
            var hostBuilder = Host.CreateDefaultBuilder(args)
                              .ConfigureHostConfiguration(configHost =>
            {
                configHost.SetBasePath(Directory.GetCurrentDirectory());
                configHost.AddJsonFile("hostsettings.json", optional: true);
                configHost.AddJsonFile($"appsettings.json", optional: false);
                configHost.AddEnvironmentVariables();
                configHost.AddEnvironmentVariables("DOTNET_");
                configHost.AddCommandLine(args);
            })
                              .ConfigureAppConfiguration((hostContext, config) =>
            {
                config.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: false);
                config.AddEnvironmentVariables();
            })
                              .ConfigureServices((hostContext, services) =>
            {
                services.UseRabbitMQMessageHandler(hostContext.Configuration);

                services.AddTransient <WorkshopManagementDBContext>((svc) =>
                {
                    var sqlConnectionString = hostContext.Configuration.GetConnectionString("WorkshopManagementCN");
                    var dbContextOptions    = new DbContextOptionsBuilder <WorkshopManagementDBContext>()
                                              .UseSqlServer(sqlConnectionString)
                                              .Options;
                    var dbContext = new WorkshopManagementDBContext(dbContextOptions);

                    DBInitializer.Initialize(dbContext);

                    return(dbContext);
                });

                services.AddHostedService <EventHandler>();
            })
                              .UseSerilog((hostContext, loggerConfiguration) =>
            {
                loggerConfiguration.ReadFrom.Configuration(hostContext.Configuration);
            })
                              .UseConsoleLifetime();

            return(hostBuilder);
        }
Beispiel #5
0
        private static void Startup()
        {
            // setup RabbitMQ
            var    configSection = Config.GetSection("RabbitMQ");
            string host          = configSection["Host"];
            string userName      = configSection["UserName"];
            string password      = configSection["Password"];

            // setup messagehandler
            RabbitMQMessageHandler messageHandler = new RabbitMQMessageHandler(host, userName, password, "Pitstop", "WorkshopManagement", "");

            // setup DBContext
            var sqlConnectionString = Config.GetConnectionString("WorkshopManagementCN");
            var dbContextOptions    = new DbContextOptionsBuilder <WorkshopManagementDBContext>()
                                      .UseSqlServer(sqlConnectionString)
                                      .Options;
            var dbContext = new WorkshopManagementDBContext(dbContextOptions);

            Policy
            .Handle <Exception>()
            .WaitAndRetry(5, r => TimeSpan.FromSeconds(5), (ex, ts) => { Log.Error("Error connecting to DB. Retrying in 5 sec."); })
            .Execute(() => DBInitializer.Initialize(dbContext));

            // start event-handler
            EventHandler eventHandler = new EventHandler(messageHandler, dbContext);

            eventHandler.Start();

            if (_env == "Development")
            {
                Log.Information("WorkshopManagement Eventhandler started.");
                Console.WriteLine("Press any key to stop...");
                Console.ReadKey(true);
                eventHandler.Stop();
            }
            else
            {
                Log.Information("WorkshopManagement Eventhandler started.");
                while (true)
                {
                    Thread.Sleep(10000);
                }
            }
        }
Beispiel #6
0
 public EventHandler(IMessageHandler messageHandler, WorkshopManagementDBContext dbContext)
 {
     _messageHandler = messageHandler;
     _dbContext      = dbContext;
 }