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:
[Route("{**all}")]
to handle all routes that are not handled by other endpoints.HttpRequestMessage
object based on the values in ControllerBase.Request
, including the Method, route, query-string, headers and body, and sent it to the other server using HttpClient
HttpResponseMessage
object I got from from HttpClient.SendAsync
I populated the values in ControllerBase.Response
, including the status code, headers and body.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 bridgedSo 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.
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.