Why middleware in ASP.NET Core requires specific semantics, but not an interface?

The Invoke method is flexible and you can ask for additional parameters. ASP.NET will inject the additional parameters using the application’s service configuration. public async Task Invoke(HttpContext ctx, IHostingEnvironment host, ISomethingElse service) { // … } C# interface definitions can’t provide this flexibility in a nice way.

Rewind request body stream

Just found one solution. Replacing the original stream with a new stream containing the data. public override async Task Invoke(IOwinContext context) { return Task.Run(() => { string body = new StreamReader(context.Request.Body).ReadToEnd(); // log body byte[] requestData = Encoding.UTF8.GetBytes(body); context.Request.Body = new MemoryStream(requestData); await this.Next.Invoke(context); }); } If you are dealing with larger amounts of data, … Read more

Global exception handling in OWIN middleware

Ok, so this was easier than anticipated, thanks for @Khalid for the heads up, I have ended up creating an owin middleware named OwinExceptionHandlerMiddleware which is dedicated for handling any exception happening in any Owin Middleware (logging it and manipulating the response before returning it to the client). You need to register this middleware as … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)