10

I've written my first ASP.NET Core Web API server. The routing is working and it is sending/receiving. I'd like to send a raw Byte array in response to a Get and I'd like to bypass any serialization (as well as any deserialization in my WPF client). For example, in my server:

[HttpGet("api/data")]
public Byte[] GetData()
{
    Byte[] data = new Byte[]{1, 2, 3, 4, 5};
    return data; // No JSON, BSON or anything else...just my array. Is it done this way?
}

In my WPF client I'd like to use the HttpClient class to receive the array - no deserialization required:

// Assume an instantiated and initialized HttpClient object called "httpCLient"
HttpResponseMessage response = httpClient.GetAsync("/api/data").Result;
// Now what?

I've scoured the internet but I haven't found an example of this, and I've been unable to figure out on my own. Could somebody please show me how this is done?

Update: I believe the client-side Byte[] retrieval can be accomplished via "HttpContent.ReadAsByteArrayAsync()" as shown here: Convert HttpContent into byte[]. I still am unsure as to how to send the raw (unserialized) Byte[] from the ASP.NET Core Web API side however.

6
  • Possible duplicate of Getting content/message from HttpResponseMessage Commented Apr 6, 2017 at 23:07
  • Thanks Colin. I think at the client side I can retrieve the Byte[] by the following: Byte[] data = httpResponseMessage.Content.ReadAsByteArrayAsync().Result; However, I cannot test this because I'm still unsure of how to send the raw Byte[] from the server. Commented Apr 6, 2017 at 23:57
  • stackoverflow.com/questions/23884182/… check Luaan's answer. Use the ByteArrayContent Commented Apr 7, 2017 at 6:38
  • Thanks Marcus. Unfortunately Luaan's answer is using the "HttpResponseMessage" class. This type doesn't seem to be supported in .NET Core...at least I can find no mention of it in the docs. Commented Apr 7, 2017 at 20:49
  • @LKeene You just need to add a refrence to the System.Net.Http NuGet package to get access to it in your ASP.NET core project. Commented Apr 7, 2017 at 21:12

1 Answer 1

8

Okay, thanks to the input I'm getting I have the following which does indeed transfer the array with the correct values:

On the server side (ASP.NET Core web api):

[HttpGet("/api/data")]
public async Task GetData()
{
    await Response.Body.WriteAsync(myByteArray, 0, myByteArray.Length);
}

On the WPF client I have:

HttpResponseMessage response = httpClient.GetAsync("/api/data").Result;
Byte[] data = response.Content.ReadAsByteArrayAsync().Result;

Frankly I don't understand how this is working (what is "Response.Body.WriteAsync...") and I don't like to blindly use code that "just works so move on". Can someone please explain how this is working, or even if this is the correct approach?

Also, is this actually bypassing serialization/deserialization? I ask because when both client and server are running on the same machine (localhost) it's taking approx. 75ms to retrieve a byte[] of length 4,194,304...that's only around 4MB and seems pretty slow for just a raw transfer of bytes.

Sign up to request clarification or add additional context in comments.

3 Comments

How do you do the opposite: Post a raw byte array to the server?
Yes, this is bypassing serialization. Response.Body.WriteAsync is the same thing that the framework eventually calls with serialized data when you use the normal pattern of returning ActionResult objects. It's probably slow because you're running debug versions in a debugger?
One thing that needs fixing is changing "async void" to "async Task".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.