My ASP.NET Web API app needs to act as an intermediate layer between clients and another HTTP resource. The fetched response from the backend HTTP service needs to pass some basic validations - but the final response needs to be the same as from the backend call.
Here's my attempt at doing this using Web API :
[HttpGet]
public async Task<HttpResponseMessage> GetSearchResults()
{
var client = new HttpClient();
var backendResponse = await client.GetAsync("http://api.duckduckgo.com/?q=DuckDuckGo&format=json");
ValidateResponseStream(await backendResponse.Content.ReadAsStreamAsync());
var myResponse = new HttpResponseMessage(HttpStatusCode.OK);
myResponse.Content = backendResponse.Content;
return myResponse;
}
While this works as intended, I think this is incorrect since I don't dispose the backendResponse.
What's a good way to dispose the backendResponse without requiring to first copy the backend response stream to a MemoryStream ?
Edit : I found this discussion thread which states explicitly that disposing a HttpResponseMessage is required; I'm still unclear whether that will happen automatically in the call above. I would also rather be explicit about Dispose being called.