Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transmit/receive raw byte array using ASP.NET Core Web API?

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.

like image 442
LKeene Avatar asked Apr 06 '17 22:04

LKeene


People also ask

How do I request body in Web API?

ReadAsStringAsync(); Trace. WriteLine(requestBody); This will log the request body.

How do you convert a byte array into a string?

There are two ways to convert byte array to String: By using String class constructor. By using UTF-8 encoding.

How to convert byte array to file c#?

Using File. The static File class in the System.IO namespace provides a simple static method WriteAllBytes() that we can use to write all the data from a byte array to a file. => File. WriteAllBytes(filePath, data); public static void SaveByteArrayToFileWithStaticMethod(byte[] data, string filePath) => File.


1 Answers

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.

like image 162
LKeene Avatar answered Oct 02 '22 23:10

LKeene