Reputation: 83
(sub-question: How to retrieve the body from a PushStreamContent in WebAPI2?)
I want to send a zipped message from one API to another API. I do this via streaming the content. I currently have the following logic:
Sending logic (with PushStreamContent sending via WebAPI2):
private async Task InitPushStreamContent(Stream stream, HttpContent content, TransportContext transportContext)
{
var tstText = "Testing 1-2-3";
var byteArray = Encoding.ASCII.GetBytes(tstText);
await new MemoryStream(byteArray).CopyToAsync(stream);
stream.Close();
}
public async Task Execute() {
using (PushStreamContent pushContent = new
PushStreamContent(InitPushStreamContent))
{
var requestMessage = new HttpRequestMessage
{
Content = pushContent
};
return await ResendMessagesOtherStage("someParameter", "someParameter",
"someParameter",
"someParemeter", requestMessage);
}
}
Retrieving logic (Another API retrieving the stream):
public async Task<IHttpActionResult> UploadSomethingAsync(HttpRequestMessage request)
{
var stream = await request.Content.ReadAsStreamAsync();
var length = (int)stream.Length;
var byteArray = new byte[length];
var message = Encoding.ASCII.GetString(byteArray, 0, byteArray.Length);
}
The message I retrieve is not:
"Testing 1-2-3"
But is:
{"Content":{"Headers":[{"Key":"Content-Type","Value":["application/octet-stream"]}]}}
I'm not sure what I'm overseeing... The same results I get when I'm retrieving a zip file.
Upvotes: 0
Views: 1116
Reputation: 83
I solved my problem by removing the HttpRequestMessage wrapper and changing the HttpClient receiver to send the request with the method PostAsync which accepts HttpContent. I previously used the generic SendAsync method which accepts HttpRequestMessage.
I think the problem was that by wrapping the content in the HttpRequestMessage object the method of the pushStreamContent was never triggered in the outgoing request. Maybe the HttpRequestMessage is not compatible with the PushStreamContent type?
Upvotes: 1