3

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();

11
  • That middleware handles incoming requests to your web server. Is this for an outgoing http request from your server code to another server? If not, can you clarify exactly which header you're trying to modify. Commented Oct 13, 2018 at 13:27
  • @sellotape yes, I need to modify the Outgoing requests. Commented Oct 13, 2018 at 13:48
  • 1
    That's unrelated to the middleware you refer to then. Assuming you're using HttpWebRequest, that header is added just before sending, with no hooks you can use to alter it. You could try set .KeepAlive = false;, which should stop the header being added, then add your own header before sending, but... there is more going on behind the scenes with keep-alive connections than simply this header. It also begs the question of why the server you're calling needs it; all the HTTP specs refer to "Keep-Alive". Commented Oct 13, 2018 at 14:40
  • FYI .KeepAlive = false; + adding my own will result in combined header Connection: keep-alive, close Commented Oct 13, 2018 at 14:47
  • 1
    @realPro - yeah, it will add "close" if KeepAlive is false. See the source here - line 5047. There doesn't seem much you can do about it. I'd complain to the provider of the server you're calling instead. Commented Oct 13, 2018 at 15:08

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

But my problem is that Invoke is not being called.
Hmm. I've had that issue before on a shared project. It was someone else's Middleware that wasn't calling next so it ended up short-circuiting the middleware in the pipeline.
1

Have you registered your middleware in the DI system? You need to do so in your Startup class, ConfigureServices method:

services.AddScoped<IMiddleware, SomeMiddleware>();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.