0

Exception occured on line : (after PrepareRequest)

var response_ = await client_.SendAsync(request_,

System.Net.Http.HttpRequestException: 'Error while copying content to a stream.'

IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host

SocketException: An existing connection was forcibly closed by the remote host

the endpoint is working well in javascript from swagger ui in my computer...

I used nswag to generate c# client code :

public virtual async System.Threading.Tasks.Task<string> UploadFileAsync(System.IO.Stream body,
            System.Threading.CancellationToken cancellationToken)
        {
            if (body == null)
                throw new System.ArgumentNullException("body");

            var urlBuilder_ = new System.Text.StringBuilder();
            urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/api/uploadFile");

            var client_ = _httpClient;
            var disposeClient_ = false;
            try
            {
                using (var request_ = new System.Net.Http.HttpRequestMessage())
                {
                    var content_ = new System.Net.Http.StreamContent(body);
                    content_.Headers.ContentType =
                        System.Net.Http.Headers.MediaTypeHeaderValue.Parse("multipart/form-data");
                    request_.Content = content_;
                    request_.Method = new System.Net.Http.HttpMethod("POST");
                    request_.Headers.Accept.Add(
                        System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));

                    PrepareRequest(client_, request_, urlBuilder_);

                    var url_ = urlBuilder_.ToString();
                    request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);

                    PrepareRequest(client_, request_, url_);

                    var response_ = await client_.SendAsync(request_,
                            System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken)
                        .ConfigureAwait(false);
                    var disposeResponse_ = true;
                    try
                    {
                        var headers_ =
                            System.Linq.Enumerable.ToDictionary(response_.Headers, h_ => h_.Key, h_ => h_.Value);
                        if (response_.Content != null && response_.Content.Headers != null)
                        {
                            foreach (var item_ in response_.Content.Headers)
                                headers_[item_.Key] = item_.Value;
                        }

                        ProcessResponse(client_, response_);

                        var status_ = (int)response_.StatusCode;
                        {
                            var objectResponse_ =
                                await ReadObjectResponseAsync<string>(response_, headers_, cancellationToken)
                                    .ConfigureAwait(false);
                            if (objectResponse_.Object == null)
                            {
                                throw new ApiException("Response was null which was not expected.", status_,
                                    objectResponse_.Text, headers_, null);
                            }

                            return objectResponse_.Object;
                        }
                    }
                    finally
                    {
                        if (disposeResponse_)
                            response_.Dispose();
                    }
                }
            }
            finally
            {
                if (disposeClient_)
                    client_.Dispose();
            }
        }

Client code is here

var httpClient = new HttpClient();
httpClient.BaseAddress = new System.Uri(@"https://server/api");

var c = new ControllerClient(httpClient);

FileInfo fi = new FileInfo(txtFilePath.Text);
var stream = File.OpenRead(fi.FullName);

var res = await c.UploadFileAsync(stream);
txtAskResult.Text = res;

i tried to solve during two hours but even with stack over flow posts i cannot find a work around working properly...

Thank you for your help !

I tried to add keep alive, change protocol, change content type...

1 Answers1

0

In fact, i use nswag on put file endpoint of pets sample of swagger UI and the code worked well. I will redefine the signature of the endpoint same than pets upload file sample. thank you