Reputation: 101
I imagine this is a pretty typical scenario: Our solution exposes an API constructed from asp core C# controllers, from which we generate a swagger.json (Using https://github.com/domaindrivendev/Swashbuckle.AspNetCore).
We call this api in code through a C# client we've generated using AutoRest (https://github.com/Azure/autorest).
To perform a potentially large upload, we'd like to use our AutoRest generated client to pass a Stream from a C# caller to our back-end, to avoid having to serialise / deserialise a complete object.
I can't work out how I might use the two tools together to pass a Stream in with a call from our c# code. I've tried adding it as a parameter, but this results in AutoRest creating a "Stream" model for System.IO.Stream, which is then used as the type of the input parameter instead of it just keeping the original type. I've tried passing it in as a [FromBody] parameter, but in addition to the issue above, AutoRest then also types it as StringContent before adding it to the request instead StreamContent (Likely because SwaggerGen doesn't identify it as a Stream?).
Would appreciate any advice - But if we can't do this we can always use a HTTPClient manually, I guess.
Upvotes: 3
Views: 1327
Reputation: 101
We were unable to solve this and ended up with a less than ideal solution that was still (To us) preferable to using a manually generated HTTPClient.
We modified the automatically generated code with the following addition:
// BEGIN MANUALLY MODIFIED CODE
_httpRequest.Content = new StreamContent(inputStream);
// ensure that Expect Continue behaviour is always used for binary submissions
_httpRequest.Headers.ExpectContinue = true;
// END MANUALLY MODIFIED CODE
Now we just use this manually generated method for all calls to that endpoint instead of the automatically generated method. This comes with a couple of significant caveats:
Upvotes: 2