Example #1
0
        public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            var env = hostingContext.HostingEnvironment;
            config.SetBasePath(env.ContentRootPath);
            config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
            config.AddJsonFile("appsettings.local.json", optional: true, reloadOnChange: true);
            config.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
            config.AddEnvironmentVariables();
        })
        .ConfigureLogging((hostContext, logging) =>
        {
            logging.ClearProviders();
            logging.AddApplicationInsights();
            logging.AddConsole();
        })
        .ConfigureServices((hostContext, services) =>
        {
            var configuration = hostContext.Configuration;

            // EF Databases
            services.AddDbContext <IPortAuthorityDbContext, PortAuthorityDbContext>(options => options
                                                                                    .UseSqlServer(configuration.GetConnectionString("SqlDatabase"),
                                                                                                  providerOptions => providerOptions.EnableRetryOnFailure()));

            // Mass Transit
            services.AddMassTransit(x =>
            {
                x.AddConsumer <CreateJobConsumer>();
                x.AddConsumer <StartJobConsumer>();
                x.AddConsumer <EndJobConsumer>();
                x.AddConsumer <CreateSubtaskConsumer>();
                x.AddConsumer <StartSubtaskConsumer>();
                x.AddConsumer <EndSubtaskConsumer>();

                x.SetKebabCaseEndpointNameFormatter();

                x.UsingRabbitMq((context, cfg) =>
                {
                    cfg.AmqpHost(configuration.GetConnectionString("Rabbit"));
                    cfg.ConfigureEndpoints(context);
                });

                PortAuthorityEndpointConventions.Map();
            });

            services.AddMassTransitHostedService();

            // Health Checks
            services.AddHealthChecks()
            .AddApplicationInsightsPublisher()
            .AddDatadogPublisher("portauthority.worker.healthchecks");

            // Application Services
            services.AddPortAuthorityServices();
            services.AddHostedService <HeartbeatBackgroundService>();
        })
        .UseConsoleLifetime();
Example #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Application Insights
            services.AddApplicationInsightsTelemetry();

            // Mvc
            services.AddCors();
            services.AddRazorPages();
            services.AddHttpContextAccessor();
            services.AddControllers(options =>
            {
                options.Conventions.UseSlugifiedRoutes();
            });

            // Runtime options providers
            services.AddOptions();
            services.Configure <CorsSettings>(Configuration.GetSection("CorsSettings"));

            // Database configuration
            services.AddDbContext <IPortAuthorityDbContext, PortAuthorityDbContext>(options => options
                                                                                    .UseSqlServer(Configuration.GetConnectionString("SqlDatabase"),
                                                                                                  providerOptions => providerOptions.EnableRetryOnFailure()));

            // MassTransit messaging endpoints
            services.AddMassTransit(x =>
            {
                x.AddConsumer <HeartbeatConsumer>();
                x.SetKebabCaseEndpointNameFormatter();
                x.UsingRabbitMq((context, cfg) =>
                {
                    cfg.AmqpHost(Configuration.GetConnectionString("Rabbit"));
                    cfg.ConfigureEndpoints(context);
                });

                PortAuthorityEndpointConventions.Map();
            });

            services.AddMassTransitHostedService();

            // Health Checks
            services.AddHealthChecks()
            .AddWorkerHeartbeats()
            .AddSqlServer(Configuration.GetConnectionString("SqlDatabase"))
            .AddRabbitMQ(rabbitConnectionString: Configuration.GetConnectionString("Rabbit"))
            .AddApplicationInsightsPublisher()
            .AddDatadogPublisher("portauthority.web.healthchecks");

            // Application services
            services.AddPortAuthorityServices();

            // Open API (Swagger) documentation
            services.AddSwaggerGen(c =>
            {
                c.AddSecurityDefinition("JSON Web Token", new OpenApiSecurityScheme()
                {
                    Name         = "Bearer",
                    Scheme       = "bearer",
                    BearerFormat = "JWT",
                    Description  = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
                    In           = ParameterLocation.Header,
                    Type         = SecuritySchemeType.Http,
                });

                var securityScheme = new OpenApiSecurityScheme()
                {
                    Reference = new OpenApiReference()
                    {
                        Id   = "JSON Web Token",
                        Type = ReferenceType.SecurityScheme
                    },
                };

                c.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    { securityScheme, new string[] { } },
                });

                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "Port Authority API", Version = "v1"
                });
            });
        }