'The inner stream position has changed unexpectedly

I am trying to upload two files from Azure Function which I have received through multipart/form-data

var formdata = await reqstring.ReadFormAsync();

var frontfile = reqstring.Form.Files["frontfile"];

var backfile = reqstring.Form.Files["backfile"];

var client_KYC_OCR = new RestClient(options); var request_KYC_OCR = new RestRequest(_KYC_Kart_URL + "/api/passport/extraction", Method.Get);

                    request_KYC_OCR.AddHeader("x-api-key", _KYC_Kart_Token);
                    request_KYC_OCR.AddHeader("Content-Type", "multipart/form-data");
                    request_KYC_OCR.AlwaysMultipartFormData = true;
                    request_KYC_OCR.AddFile("front", getFile: frontfile.OpenReadStream, frontfile.FileName, frontfile.ContentType);
                    request_KYC_OCR.AddFile("back", getFile: backfile.OpenReadStream, backfile.FileName, backfile.ContentType);
                    

                    RestResponse response_KYC_OCR = await client_KYC_OCR.ExecuteAsync(request_KYC_OCR);

But I am getting the ERROR:

"System.Net.Http.HttpRequestException: An error occurred while sending the request. System.InvalidOperationException: The inner stream position has changed unexpectedly. at Microsoft.AspNetCore.Http.ReferenceReadStream.VerifyPosition() at Microsoft.AspNetCore.Http.ReferenceReadStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken) at System.IO.Stream.CopyToAsyncInternal(Stream destination, Int32 bufferSize, CancellationToken cancellationToken) at System.Net.Http.HttpContent.CopyToAsyncCore(ValueTask copyTask) at System.Net.Http.MultipartContent.SerializeToStreamAsyncCore(Stream stream, TransportContext context, CancellationToken cancellationToken) at System.Net.Http.HttpContent.CopyToAsyncCore(ValueTask copyTask) at System.Net.Http.HttpConnection.SendRequestContentAsync(HttpRequestMessage request, HttpContentWriteStream stream, CancellationToken cancellationToken) at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) End of inner exception stack trace at RestSharp.RestClient.ThrowIfError(RestResponse response) at RestSharp.RestClient.ExecuteAsync(RestRequest request, CancellationToken cancellationToken) at Urbanmoney_AZ.Functions.KYC_Update.Run(HttpRequestMessage req, HttpRequest reqstring, ILogger log)

I tried googling the same but not found any Eact Solution for the same

Can anyone help me out please



Solution 1:[1]

  • As 'per this Github Issue "The inner stream position has changed unexpectedly" exception may cause because of the sequential order. Try opening and processing files in sequential order.

  • Also as suggested by Bandook try synchronous execution method rather than execute Async method

  • for further details check this SO thread.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SaiSakethGuduru-MT