By exe


2018-07-30 06:58:58 8 Comments

My C# app uploads file to some API, I'm using multipart request, i.e I'm uploading a json string and binary contect of the file, it works fine for most files, but for very few it gives exception, I mean let's try for file named myFile128MB.dat I'm getting the exception:

::: Error while copying content to a stream. ::: Unable to read data from the transport connection: A request to send or receive data was disallowed because the socket had already been shut down in that direction with a previous shutdown call. ::: System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.IO.IOException: Unable to read data from the transport connection: A request to send or receive data was disallowed because the socket had already been shut down in that direction with a previous shutdown call. ---> System.Net.Sockets.SocketException: A request to send or receive data was disallowed because the socket had already been shut down in that direction with a previous shutdown call at System.Net.Sockets.Socket.BeginReceive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, AsyncCallback callback, Object state) at System.Net.Sockets.NetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state) --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state) at System.Net.FixedSizeReader.StartReading() at System.Net.Security._SslStream.StartFrameHeader(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security._SslStream.StartReading(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security._SslStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security._SslStream.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback asyncCallback, Object asyncState) at System.Net.TlsStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback asyncCallback, Object asyncState) at System.Net.ConnectStream.BeginReadWithoutValidation(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state) at System.Net.ConnectStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state) at System.Net.Http.HttpClientHandler.WebExceptionWrapperStream.BeginRead(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state) at System.Net.Http.StreamToStreamCopy.StartRead() --- End of inner exception stack trace --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at Lib.CheckPoint.ThreatPrevention.d__19.MoveNext() in D:\New folder\myApp.cs

My code looks roughly as follows:

public async Task<Dictionary<string , string>> Upload(string filePath)
{   
    var fi             = new FileInfo(FilePath);
    var jsonString     = "some json string";
    var fileContents   = File.ReadAllBytes(fi.FullName);
    var webService     = new Uri(url);
    var requestMessage = new HttpRequestMessage(HttpMethod.Post , webService);

    requestMessage.Method = HttpMethod.Post;
    requestMessage.Headers.Add("Authorization" , "MyKey1234");

    const string boundry = "------------------My-Boundary";
    var multiPartContent = new MultipartFormDataContent(boundry);
    var byteArrayContent = new ByteArrayContent(fileContents);
    multiPartContent.Add(byteArrayContent);

    var.Content = multiPartContent;
    var httpClient = new HttpClient();

    HttpResponseMessage httpResponse = await httpClient.SendAsync(requestMessage , HttpCompletionOption.ResponseContentRead , CancellationToken.None);
    //exception in this line ^
    return new Dictionary<string , string>();
}

The caller:

myDictionary = await Upload(filePath);

The structure of the console app is as follows:

public class Program
{
    public static void Main(string[] args) => MainAsync().Wait();
    private static async Task MainAsync()  => new MyClass().Start();
}

And inside MyClass:

public async void Start() => myDictionary = await Upload(filePath);

0 comments

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] System.IO.IOException: The handshake failed due to an unexpected > packet format?

  • 2011-03-03 09:21:56
  • TheLearner
  • 35284 View
  • 18 Score
  • 3 Answer
  • Tags:   c# wcf

0 Answered Questions

WCF service - connection was forcibly closed by the remote host

  • 2014-06-12 13:32:11
  • SelvirK
  • 2415 View
  • 0 Score
  • 0 Answer
  • Tags:   c# asp.net .net wcf

1 Answered Questions

0 Answered Questions

PowerShell + HttpWebRequest throws exception

0 Answered Questions

The message received was unexpected or badly formatted

2 Answered Questions

[SOLVED] Accessing Webservice which is hosted on https server

2 Answered Questions

[SOLVED] How to properly call an external WCF Service from SharePoint 2010?

1 Answered Questions

How do access a secure website within a sharepoint webpart?

Sponsored Content