コード例 #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public virtual void Configure(IApplicationBuilder app, IWebHostEnvironment env, IConfiguration config)
        {
            // Needed for a ReDoc logo
            const string LOGO_FILE_PATH = "wwwroot/swagger";
            var          fileprovider   = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, LOGO_FILE_PATH));
            var          requestPath    = new PathString($"/{LOGO_FILE_PATH}");

            app.UseDefaultFiles(new DefaultFilesOptions
            {
                FileProvider = fileprovider,
                RequestPath  = requestPath,
            });

            app.UseFileServer(new FileServerOptions()
            {
                FileProvider            = fileprovider,
                RequestPath             = requestPath,
                EnableDirectoryBrowsing = false
            });

            app.UseStaticFiles();

            // Register ReDoc middleware
            app.UseReDocMiddleware(config);

            // Register Swagger and SwaggerUI middleware
            app.UseSwaggerMiddleware(config);

            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();

            // For elevated security, it is recommended to remove this middleware and set your server to only listen on https.
            // A slightly less secure option would be to redirect http to 400, 505, etc.
            app.UseHttpsRedirection();

            // NOTE** Add logging middleware(s) only when not runnig from integration/unit tests!
            if (!UnitTestDetector.IsRunningFromUnitTest())
            {
                // Adds request/response logging middleware
                app.UseMiddleware <RequestResponseLoggingMiddleware>();

                // Adds middleware for streamlined request logging
                app.UseSerilogRequestLogging(options =>
                {
                    // Customize the message template
                    options.MessageTemplate         = "{Host} {Protocol} {RequestMethod} {RequestPath} {EndpointName} {ResponseBody} responded {StatusCode} in {Elapsed} ms";
                    options.EnrichDiagnosticContext = RequestLogHelper.EnrichDiagnosticContext;
                });
            }

            // Adds global error handling middleware
            app.UseApiExceptionHandling();

            // Adds enpoint routing middleware
            app.UseRouting();

            // Adds a CORS middleware
            app.UseCors("EnableCORS");

            // These are the important ones - order matters!
            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                //Add health check endpoint
                endpoints.MapHealthChecks("/healthz");
                // Adds enpoints for controller actions without specifyinf any routes
                endpoints.MapControllers();
            });
        }
コード例 #2
0
 static TestHost()
 {
     UnitTestDetector.SetIsInUnitTest();
 }