Reputation: 1846
All I need to do is to modify [Connection]
HTTP Header from "Keep-alive" to lowercase "keep-alive".
I wrote the class,
public class PreRequestModifications
{
private readonly RequestDelegate _next;
public PreRequestModifications(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
// Does not get called when making an HTTPWebRequest.
await _next.Invoke(context);
}
}
and registered on startup,
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseMiddleware<PreRequestModifications>();
}
but the Invoke
method does not get called when I execute await httpWebRequest.GetResponseAsync();
Upvotes: 2
Views: 14841
Reputation: 307
Have you registered your middleware in the DI system? You need to do so in your Startup
class, ConfigureServices
method:
services.AddScoped<IMiddleware, SomeMiddleware>();
Upvotes: 1
Reputation: 378
So middleware gets hit when a request is made and when a response is sent back. Effectively this means you can move through the Invoke method twice like so:
public async Task Invoke(HttpContext context)
{
ModifyRequest(context);
await _next(context);
ModifyResponse(context);
}
So you can modify the response in the ModifyResponse
method.
Microsoft's documentation will make it clearer: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-2.1
Hopefully this helps.
Upvotes: 1