2

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

Nkosi
  • 235,767
  • 35
  • 427
  • 472
realPro
  • 1,713
  • 3
  • 22
  • 34
  • 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. – sellotape Oct 13 '18 at 13:27
  • @sellotape yes, I need to modify the Outgoing requests. – realPro Oct 13 '18 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". – sellotape Oct 13 '18 at 14:40
  • FYI .KeepAlive = false; + adding my own will result in combined header Connection: keep-alive, close – realPro Oct 13 '18 at 14:47
  • When you say you want to modify the outgoing requests do you mean... the responses? – Christopher Lake Oct 13 '18 at 14:48
  • @ChristopherLake no I need to modify the request made by HttpWebRequest from a service. – realPro Oct 13 '18 at 14:50
  • But doesn't this line `await httpWebRequest.GetResponseAsync();` explicitly say get `response`? :P See my answer below. – Christopher Lake Oct 13 '18 at 14:52
  • 1
    @realPro - yeah, it will add "close" if `KeepAlive` is false. See the source [here](https://github.com/Microsoft/referencesource/blob/master/System/net/System/Net/HttpWebRequest.cs) - 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. – sellotape Oct 13 '18 at 15:08
  • @sellotape the provider of server i'm calling is Google – realPro Oct 13 '18 at 15:12
  • I'd be fairly surprised if they really have this requirement. What is indicating that the case of the "keep-alive" text is the issue? – sellotape Oct 13 '18 at 15:17
  • @sellotape not an issue, I just find it strange that there is something in the code that "can't be done". – realPro Oct 13 '18 at 16:00

2 Answers2

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.

Christopher Lake
  • 358
  • 4
  • 10
  • But my problem is that Invoke is not being called. – realPro Oct 13 '18 at 14:54
  • 1
    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. – Christopher Lake Oct 13 '18 at 14:56
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>();
Saeed Ganji
  • 197
  • 17