1

I have a 4.7.2 application and I'm trying to rewrite it in .net core 3.1.

I have a method in a controller below. Of course, the real code is different I receive some parameters and generate a URL, etc. But the idea is the same. I am trying to reflect another server's response.

    [Route("images")]
    public async Task<HttpResponseMessage> GetImage()
    {
        Uri uri = new Uri("https://img-lcwaikiki.mncdn.com/mnresize/1024/-/pim/productimages/20202/4353414/l_20202-0w9011z8-hrz_a.jpg", UriKind.Absolute);
        using (HttpClient client = new HttpClient())
        {
            return await client.GetAsync(uri);
        }
    }

But interestingly, .net framework and core act totally different.

Framework, returns the image as I expected (.net Framework 4.7.2 Sample).

But core returns a json in the body (.net Core 3.1 Sample).

I've checked the Microsoft Documentation, they are the same for Sytem.Net.Http.HttpClient class both in netCore 3.1 and .net Framework 4.7.2.

To reproduce you can create a fresh netCore and .netFramework apps. BTW I've created a repo for this projects: https://github.com/fkucuk/imagereflectorhttpclient

8
  • Post any code in the question itself. The code should actually reproduce the problem It's the server that returns different responses, not HttpClient. BTW both links return JSON in Chrome Commented Sep 23, 2020 at 9:36
  • Check this post stackoverflow.com/questions/40794275/… Commented Sep 23, 2020 at 9:37
  • @avhornet how is this relevant? According to the question in both links the server returns JSON, not an image. Commented Sep 23, 2020 at 9:37
  • @avhornet although, running this code in .NET Core returns an image ... Commented Sep 23, 2020 at 9:41
  • No repro. This code returns an image in .NET Core as well. In fact, the screenshots look like a dump of the response object, not actual JSON responses Commented Sep 23, 2020 at 9:44

2 Answers 2

0

Looks like treatment of HttpResponseMessage returned from client.GetAsync is different between .NET Framework and .NET Core framework.

While .NET Framework either returns the HttpResponseMessage as it is to the API client or retrieves the contents and wraps it into another HttpResponseMessage before returning it to the client, .NET Core considers it as an object and uses default media converter (JSON here) to transform it and wraps it again to another HttpResponseMessage and returns to the client of your API.

Also with the latest development in .NET Core, HttpResponseMessage is less likely used for Web API. You can use IActionResult which can be use to return pretty much any kind of response from the API.

You can read more about it here

To solve your problem, following is my recommendation.

[HttpGet]
public async  Task<IActionResult> Get()
{
    Uri uri = new Uri("https://img-lcwaikiki.mncdn.com/mnresize/1024/-/pim/productimages/20202/4353414/l_20202-0w9011z8-hrz_a.jpg", UriKind.Absolute);
    using (HttpClient client = new HttpClient())
    {
        using (var stream = await client.GetStreamAsync(uri))
        {
            return File(stream, "image/jpeg");
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks Chetan, The solution you provided does not return the file itself I guess.

I've came up with the following solution.

But I'm not sure if this will cause a leak. I'm not disposing the stream.

        [HttpGet]
        [Route("v1")]
        public async Task<IActionResult> GetImage1()
        {
            Uri uri = new Uri("https://img-lcwaikiki.mncdn.com/mnresize/1024/-/pim/productimages/20202/4353414/l_20202-0w9011z8-hrz_a.jpg", UriKind.Absolute);
            using (HttpClient client = new HttpClient())
            {
                var response = await client.GetAsync(uri);
                var stream = await response.Content.ReadAsStreamAsync();
                return File(stream, "image/jpeg");
            }
        }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.