title: How to capture BadHttpRequestException in ASP.NET Core
body: <h2>Summary</h2>
  <p>I'm struggling to find a way to capture low-level HTTP exceptions thrown by Kestrel in an ASP.NET Core application. I can see minimal information about them in the console (<code>BadHttpRequestException: Malformed request: invalid headers</code>) but ideally I'd like to be able to place a breakpoint somewhere so I can analyse these requests in detail.</p>
  <h2>Background</h2>
  <p>I'm trying to create a LabVIEW application that communicates with an ASP.NET Core-based webservice I wrote. LabVIEW itself doesn't directly support websocket communication, and a library I discovered to add support for this isn't working correctly so I'm trying to manually construct the TCP message I need to send to perform the upgrade.</p>
  <p>If I try to open a websocket connection to my webservice using, for example, Firefox's Simple WebSocket Client extension, then the connection succeeds and I can communicate with my webservice through it just fine. I can also use Firefox's dev tools to inspect the HTTP upgrade request. However, if I try to send a similarly-constructed message from LabVIEW using its TCP library, the connection fails and I see a <code>BadHttpRequestException</code> appear in my webservice's console complaining about invalid headers.</p>
  <h2>What I've Tried</h2>
  <p>My first approach was to try adding middleware to capture the requests being made from LabVIEW. In my <code>Startup</code> class, I added the following very basic middleware so I could essentially break on requests the moment they enter the pipeline:</p>
  <pre><code>app.Use(async (ctx, next) =&gt;
  {
      await next();
  });
  </code></pre>
  <p>This was placed above all other configured middleware. Unfortunately, control flow never reached this point when sending requests from LabVIEW, and I assume this is because the middleware pipeline won't get triggered if a low-level exception like <code>BadHttpRequestException</code> gets thrown.</p>
  <p>My second attempt was using Fiddler. This didn't help either, as it wasn't showing any traffic coming from LabVIEW.</p>
  <p>This is the LabVIEW code I'm using to perform the initial handshake:</p>
  <p><img src="https://i.stack.imgur.com/5cwYs.png" alt="" /></p>
  <p>This code attempts to open a connection to localhost on port 36364 (which succeeds) and then send the specified HTTP GET request over it, ensuring that CRLF is used for all line endings.</p>