Beispiel #1
0
        /// <summary>
        /// The configure method called by the runtime; use this method to configure the HTTP request pipeline.
        /// </summary>
        /// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
        /// <param name="clientFactory">The <see cref="IHttpClientFactory"/>.</param>
        public void Configure(IApplicationBuilder app, IHttpClientFactory clientFactory)
        {
            // Register the ServiceAgent HttpClientCreate (for cross-domain calls) so it uses the factory.
            WebApiServiceAgentManager.RegisterHttpClientCreate((rd) =>
            {
                var hc         = clientFactory.CreateClient(rd.BaseAddress.AbsoluteUri);
                hc.BaseAddress = rd.BaseAddress;
                return(hc);
            });

            // Add exception handling to the pipeline.
            app.UseWebApiExceptionHandler();

            // Add Swagger as a JSON endpoint and to serve the swagger-ui to the pipeline.
            app.UseSwagger();
            app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Company.AppName"));

            // Add health checks page to the pipeline.
            app.UseHealthChecks("/health");

            // Add execution context set up to the pipeline.
            app.UseExecutionContext();

            // Use controllers.
            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
Beispiel #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IConfiguration config, ILoggerFactory loggerFactory, IHttpClientFactory clientFactory)
        {
            // Configure the logger.
            _logger = loggerFactory.CreateLogger("Logging");
            Logger.RegisterGlobal((largs) => WebApiStartup.BindLogger(_logger, largs));

            // Register the HttpClientCreate so it uses the factory.
            WebApiServiceAgentManager.RegisterHttpClientCreate((rd) =>
            {
                var hc         = clientFactory.CreateClient(rd.BaseAddress.AbsoluteUri);
                hc.BaseAddress = rd.BaseAddress;
                return(hc);
            });

            // Override the exception handling.
            WebApiExceptionHandlerMiddleware.IncludeUnhandledExceptionInResponse = config.GetValue <bool>("BeefIncludeExceptionInInternalServerError");
            app.UseWebApiExceptionHandler();

            // Set up the health checks.
            app.UseHealthChecks("/health");

            // Enable middleware to serve generated Swagger as a JSON endpoint and serve the swagger-ui.
            app.UseSwagger();
            app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "BEEF Demo"));

            // Configure the ExecutionContext for the request.
            app.UseExecutionContext((context, ec) =>
            {
                ec.Username  = context.User.Identity.Name ?? WebApiExecutionContextMiddleware.DefaultUsername;
                ec.Timestamp = DateTime.Now;
            });

            // Use mvc.
            app.UseMvc();
        }
Beispiel #3
0
        /// <summary>
        /// The configure method called by the runtime; use this method to configure the HTTP request pipeline.
        /// </summary>
        /// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
        /// <param name="clientFactory">The <see cref="IHttpClientFactory"/>.</param>
        public void Configure(IApplicationBuilder app, IHttpClientFactory clientFactory)
        {
            // Register the ServiceAgent HttpClientCreate (for cross-domain calls) so it uses the factory.
            WebApiServiceAgentManager.RegisterHttpClientCreate((rd) =>
            {
                var hc         = clientFactory.CreateClient(rd.BaseAddress.AbsoluteUri);
                hc.BaseAddress = rd.BaseAddress;
                return(hc);
            });

            // Add exception handling to the pipeline.
            app.UseWebApiExceptionHandler();

            // Add Swagger as a JSON endpoint and to serve the swagger-ui to the pipeline.
            app.UseSwagger();
            app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Cdr.Banking"));

            // Add health checks page to the pipeline.
            app.UseHealthChecks("/health");

            // Add execution context set up to the pipeline.
            app.UseExecutionContext((hc, ec) =>
            {
                // TODO: This would be replaced with appropriate OAuth integration, etc... - this is purely for illustrative purposes only.
                if (!hc.Request.Headers.TryGetValue("cdr-user", out var username) || username.Count != 1)
                {
                    throw new Beef.AuthorizationException();
                }

                var bec = (ExecutionContext)ec;

                switch (username[0])
                {
                case "jessica":
                    bec.Accounts.AddRange(new string[] { "12345678", "34567890", "45678901" });
                    break;

                case "jenny":
                    bec.Accounts.Add("23456789");
                    break;

                case "jason":
                    break;

                default:
                    throw new Beef.AuthorizationException();
                }
            });

            // Use controllers.
            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
Beispiel #4
0
        /// <summary>
        /// The configure method called by the runtime; use this method to configure the HTTP request pipeline.
        /// </summary>
        /// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
        /// <param name="env">The <see cref="IHostingEnvironment"/>.</param>
        /// <param name="config">The <see cref="IConfiguration"/>.</param>
        /// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
        /// <param name="clientFactory">The <see cref="IHttpClientFactory"/>.</param>
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IConfiguration config, ILoggerFactory loggerFactory, IHttpClientFactory clientFactory)
        {
            // Configure the logger.
            _logger = loggerFactory.CreateLogger("Logging");
            Logger.RegisterGlobal((largs) => WebApiStartup.BindLogger(_logger, largs));

            // Register the HttpClientCreate so it uses the factory.
            WebApiServiceAgentManager.RegisterHttpClientCreate((rd) =>
            {
                var hc         = clientFactory.CreateClient(rd.BaseAddress.AbsoluteUri);
                hc.BaseAddress = rd.BaseAddress;
                return(hc);
            });

            // Enable middleware to serve generated Swagger as a JSON endpoint.
            app.UseSwagger();

            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Company.AppName");
            });

            // Override the exception handling.
            var includeExceptionInInternalServerError = config.GetValue <bool>("BeefIncludeExceptionInInternalServerError");

            app.UseExceptionHandler(c => WebApiStartup.ExceptionHandler(c, includeExceptionInInternalServerError));

            // Configure the ExecutionContext for the request.
            app.UseExecutionContext((context, ec) =>
            {
                ec.Username  = context.User.Identity.Name ?? "Anonymous";
                ec.Timestamp = DateTime.Now;
            });

            app.UseMvc();
        }