Search code examples
c#.netasp.net-coreasp.net-web-apidotnet-httpclient

How to bridge requests from one service to another in ASP.NET


I'm developing a service in ASP.NET Core Web API which acts as a proxy for another service. For few specific endpoints it should handle the requests itself, but for all others endpoints I want it to act as a bridge to the other service, sending the request as-is, and also returning its responses as is.

I kind of managed to do that, but it becomes pretty complex and I keep finding edge cases that need special treatment, even though I believed it should be a pretty straightforward problem. So I wonder if I'm missing a simpler solution.

Here's the gist of what I tried to do:

Some issues that I encountered (the first 2 I resolved, the last one I haven't tried yet, as I figured that it's getting too complicated)

  • ResponsePhrase wasn't bridged
  • When sending a request from a browser, I got error 404 and had to remove the original Host header in order to solve that
  • If response body is not very small, (e.g. Swagger HTML) it was not bridged correctly due to chunking error.

So am I missing some simpler solution? If it's really complex, isn't there any existing method or library that already does it?

Your help is highly appreciated.


Solution

  • I think that YARP is something that you are looking for :

    https://microsoft.github.io/reverse-proxy/api/index.html

    It has everything you need (and more) - we managed to put swagger, even hangfire dashboard through it.