asp.net-core-middleware

difference between Map and MapWhen branch in asp.net core Middleware?

纵然是瞬间 提交于 2020-07-17 10:01:24
问题 When to use Map and MapWhen branch in asp.net core middleware while we are authenticating request. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.Map("", (appBuilder) => { appBuilder.Run(async (context) => { await context.Response.WriteAsync(""); }); }); app.MapWhen(context => context.Request.Query.ContainsKey(""), (appBuilder) => { appBuilder.Run(async (context) => { await context.Response.WriteAsync(""); }); }); } 回答1: Map could branch the request based on

difference between Map and MapWhen branch in asp.net core Middleware?

霸气de小男生 提交于 2020-07-17 10:00:04
问题 When to use Map and MapWhen branch in asp.net core middleware while we are authenticating request. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.Map("", (appBuilder) => { appBuilder.Run(async (context) => { await context.Response.WriteAsync(""); }); }); app.MapWhen(context => context.Request.Query.ContainsKey(""), (appBuilder) => { appBuilder.Run(async (context) => { await context.Response.WriteAsync(""); }); }); } 回答1: Map could branch the request based on

Modify middleware response

♀尐吖头ヾ 提交于 2020-05-10 07:29:09
问题 My requirement: write a middleware that filters all "bad words" out of a response that comes from another subsequent middleware (e.g. Mvc). The problem: streaming of the response. So when we come back to our FilterBadWordsMiddleware from a subsequent middleware, which already wrote to the response, we are too late to the party... because response started already sending, which yields to the wellknown error response has already started ... So since this is a requirement in many various

How to get actual request execution time

跟風遠走 提交于 2020-01-22 09:37:25
问题 Given the following middleware: public class RequestDurationMiddleware { private readonly RequestDelegate _next; private readonly ILogger<RequestDurationMiddleware> _logger; public RequestDurationMiddleware(RequestDelegate next, ILogger<RequestDurationMiddleware> logger) { _next = next; _logger = logger; } public async Task Invoke(HttpContext context) { var watch = Stopwatch.StartNew(); await _next.Invoke(context); watch.Stop(); _logger.LogTrace("{duration}ms", watch.ElapsedMilliseconds); } }

How to get actual request execution time

淺唱寂寞╮ 提交于 2020-01-22 09:37:06
问题 Given the following middleware: public class RequestDurationMiddleware { private readonly RequestDelegate _next; private readonly ILogger<RequestDurationMiddleware> _logger; public RequestDurationMiddleware(RequestDelegate next, ILogger<RequestDurationMiddleware> logger) { _next = next; _logger = logger; } public async Task Invoke(HttpContext context) { var watch = Stopwatch.StartNew(); await _next.Invoke(context); watch.Stop(); _logger.LogTrace("{duration}ms", watch.ElapsedMilliseconds); } }

How to add model to PredictionEnginePool in middleware (ML.NET)?

痴心易碎 提交于 2020-01-15 14:27:28
问题 I'm using ML.NET in an ASP.NET Core application, and I am using the following code in Startup : var builder = services.AddPredictionEnginePool<Foo, Bar>(); if (File.Exists("model.zip")) { builder.FromFile(String.Empty, "model.zip", true); } If model.zip doesn't exist, I create it later in the middleware. How do I add it to the PredictionEnginePool that is injected? There are no options to load a model via PredictionEnginePool , and instantiating or injecting a PredictionEnginePoolBuilder isn

How to add model to PredictionEnginePool in middleware (ML.NET)?

核能气质少年 提交于 2020-01-15 14:25:11
问题 I'm using ML.NET in an ASP.NET Core application, and I am using the following code in Startup : var builder = services.AddPredictionEnginePool<Foo, Bar>(); if (File.Exists("model.zip")) { builder.FromFile(String.Empty, "model.zip", true); } If model.zip doesn't exist, I create it later in the middleware. How do I add it to the PredictionEnginePool that is injected? There are no options to load a model via PredictionEnginePool , and instantiating or injecting a PredictionEnginePoolBuilder isn

How to use IApplicationBuilder middleware overload in ASP.NET Core

偶尔善良 提交于 2020-01-11 09:16:29
问题 ASP.NET Core provides two overloads for app.Use() method. Usually we use only one overload that is app.Use(Func<HttpContext,Func<Task>, Task> middleware) Which is used as app.Use(async (context, next) => { await context.Response.WriteAsync("1st middleware <br/>"); await next.Invoke(); }); The other overload that i want to use is app.Use(Func<RequestDelegate,RequestDelegate> middleware) I couldn't find an example of how we can use this overload. Any ideas will be great. 回答1: Func

Injecting Service in Middleware in ASP.NET Core

做~自己de王妃 提交于 2020-01-01 04:51:08
问题 I want to inject a service based on the HTTP header value. So I have 2 classes - DbDataProvider and InMemDataProvider, both are implemented from IDataProvider. Whenever an API call is made, a header is passed by the client which determines whether DbDataProvider is required or InMemDataProvider is required. How do I achieve that? So in short I need to inject service in the ServiceCollection in one of the middlewares. Is that possible? The problem is that in the ConfigureService method in