Using Web API 2.2, suppose I want to read from HttpContent twice, each time as a different type.
await httpContent.LoadIntoBufferAsync(); //necessary to buffer content for multiple reads var X = await httpContent.ReadAsAsync<T>(); //read as first type var Y = await httpContent.ReadAsAsync<Dictionary<string, object>>(); //read as second type When I run the above code, X is a non-null instance of T while Y is null. If I switch the order, Y will be a non-null dictionary while X will be null. In other words, the second and subsequent calls to ReadAsAsync will always return null unless they're called with the same generic type parameter. Independently, either call to ReadAsAsync works as expected (even when needlessly calling LoadIntoBufferAsync).
This is unexpected to me - it seems that I should be able to read buffered content as differing types as many times as I want. If I add another line:
var Z = await httpContent.ReadAsString(); The result is Z will be a non-null string, no matter the order of assignment to X, Y, Z.
So how come this happens, and why can't I read from HttpContent using ReadAsAsync with multiple types?
Tin order to implement partial updates /PATCHin Web API. The second round of reading to a dictionary gives me a list of keys (property names) to overwrite with the submitted data. I am aware of the ODataDelta<T>class, but unfortunately it doesn't appear to work correctly outside of OData controllers. stackoverflow.com/questions/15561874/…