Reputation: 6258
I have a simple middleware:
public class MiddlewareInterceptor
{
RequestDelegate _next;
public MiddlewareInterceptor(RequestDelegate next)
{
_next = next;
}
public Task Invoke(HttpContext ctx)
{
ctx.Response.WriteAsync("<h2>From SomeMiddleWare</h2>");
return _next(ctx);
}
}
And in my Startup.cs Configure method, I hook it like so:
app.UseMiddleware<MiddlewareInterceptor>();
The above builds and the app seems to run fine, but my breakpoint in the interceptor Invoke method never hits. And likewise, there is never any output. I tried that with Debug.WriteLine
also.
Now, I also tried this method:
public class MiddlewareInterceptor : OwinMiddleware
{
public MiddlewareInterceptor(OwinMiddleware next) : base(next){}
public override async Task Invoke(IOwinContext context)
{
Debug.WriteLine(context.Request.Uri.ToString());
await Next.Invoke(context);
}
}
And in my Startup.cs Configure method, I hook it like so:
app.Use(next => new MiddlewareInterceptor(next).Invoke);
Unfortunately, the base OwinMiddleware
constructor is looking for the next OwinMiddleware
as a parameter, unlike ye olde RequestDelegate
. So my app.Use
instantiation of my MiddlewareInterceptor
fails because next
is of type RequestDelegate
.
Lastly, I have tried an inline function directly in the Configure method which also never hits the breakpoint:
app.Use(async (ctx, next) =>
{
System.Diagnostics.Debug.WriteLine("Hello");
await next();
});
So as it stands, it seems like I cant make a basic middleware interceptor using OWIN. What am I missing?
Upvotes: 1
Views: 361
Reputation: 66
What is the order of the aforementioned middleware in the pipeline? Make sure this executes before anything that will terminate the request part of the pipeline; eg. UseMvc();
Upvotes: 5