0

I have created a class library test project that has one class which implements the IhttpModule interface.

I have a demo web api project that is hosted on my local IIS and has an HttpPost method which inside uses a class that makes outgoing Http requests at third parties.

As of now it works like a charm for the incoming requests but i want to catch every request that comes and goes from my API. I do not care about the responses.

I have already checked the requests made at the third parties and they are correct.

API's web.config :

<system.webServer>
  <modules>
    <add name="MyIISModule" type="IisTestProject.MyIISModule" />
  </modules>
</system.webServer>

MyIISModule.cs :

    public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(OnBeginRequest);
    }


    private void OnBeginRequest(object sender, EventArgs e)
    {
        var app = (HttpApplication) sender;
        File.AppendAllLines("C:\\requestHeaders.txt",  new List<string>() {$"{DateTime.Now.ToOADate()}_{app.Context.Request.ToString()}" });
    }

    public void Dispose()
    {

    }

I made this so i can just check whenever a request happens.

Is there any way to do this without changing my API's code and using only my HttpModule?

Also i have found this:

HTTP modules can only see requests going through IIS/ASP.NET pipeline, while such outbound requests go through Windows sockets directly, and not through IIS/ASP.NET pipeline.

Is there any way to circumvent this?

  • Unrelated observation: I don't think `File.AppendAllLines` is thread safe. If not, this code is very fragile because you are almost certain to receive more than one request at a time. – Crowcoder Feb 18 '20 at 13:22
  • It actually does not cause a problem right now. I have copied the .dll generated from the test project along with the .pdb file to the bin directory of the API project and the break point triggers whenever i call my POST method, the requests that are generated from this call though do not trigger my breakpoint. My point is that even though this could cause an error i should have at least the breakpoint triggered. – Nickolas Lamp Feb 18 '20 at 13:39
  • What kind of outgoing requests are you trying to capture? Ones you make from your code with something like `HttpClient` or `WebClient`? – stuartd Feb 18 '20 at 14:36
  • @stuartd correct. I am trying to capture requests that are made using HttpClient from my code. – Nickolas Lamp Feb 19 '20 at 07:39

0 Answers0