Z
Z
Zakharov Alexander2018-10-01 17:48:13
C++ / C#
Zakharov Alexander, 2018-10-01 17:48:13

C# Request.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult() can be called twice in a row on the same Request?

Hello.
I process the body of the POST request on the server and get the content using the method:
byte[] arr_body = Request.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult();
5bb2323fa37f4063368025.pngIsn't it a mistake to call the input stream reading methods again after applying such a method, for example:
NameValueCollection nvc = Request.Content.ReadAsFormDataAsync().GetAwaiter().GetResult();
or
MultipartMemoryStreamProvider multipart_request = Request.Content.ReadAsMultipartAsync().GetAwaiter().GetResult();
In principle, the circuit works and everything is read a second time, but is this really permissible?
PS
In Java, such a number did not pass, and if I read the data from the Request once, then an exception was already thrown the second time.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
basrach, 2018-10-01
@AlexZaharow

In fact, these methods reread the stream. In this case, the "stream" is purely an abstraction, and the data actually lies in memory. Basically, if you call ReadAsStreamAsync on Response.Content, read that stream and close it, and then try to read it again, you will also get an exception. In dot.net specifically, these two methods are implemented in such a way that they do not close the stream after use, but read it from the beginning each time.
Is this allowed? From a purely technical point of view, if it works, then why not. And not with a technical need to be guided by the principle of least surprise.

S
sunblossom, 2018-10-01
@sunblossom

You pay with CPU time, saving RAM. Only and everything. It already depends on what you have more.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question