1

My function is below. It works just great with anything up to around 25MB, but greater than that and it stops working. When I say stops working, it throws no exceptions and fails to the noserver result option at the bottom of the function. I can't seem to find any settings referring to any other buffer size though I found some Microsoft Documentation which does say the response buffer is 2GB default.

Any ideas, any help would be appreciated.

  • VS2019 ASP.NET
  • 4.7.2
  • Winforms

    public async Task<JsonApiResult> GetHttpData(string Message, string serviceMethod)
    {
        System.Diagnostics.Debug.Print($"{serviceMethod}{Message}");
    
        HttpClient _httpClient = new HttpClient();
    
        _httpClient.DefaultRequestHeaders.Accept.Clear();
        _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/text"));
    
        _httpClient.BaseAddress = new Uri(_url);
    
        try
        {
            //////
            StringContent payload = new StringContent(Message, Encoding.UTF8, "application/json");
    
            HttpResponseMessage getResponse = await _httpClient.PostAsync(serviceMethod, payload);
    
            if (getResponse.IsSuccessStatusCode)
            {
                try
                {
                    var response = getResponse.Content.ReadAsStringAsync().Result;
    
                    JsonApiResult result = JsonConvert.DeserializeObject<JsonApiResult>(response);
    
                    return result;
                }
                catch (Exception e)
                {
                    return new JsonApiResult { Result = "fail", Message = e.Message, Data = "" };
                }
            }
            else
            {
                //<<<<<<<<<<<<<<<<<<<<<<<<<
                return new JsonApiResult { Result = "fail", Message = getResponse.StatusCode.ToString(), Data = "" };
            }
        }
        catch (Exception e)
        {
            return new JsonApiResult { Result = "errserver", Message = e.Message, Data = "" };
        }
    
        return new JsonApiResult { Result = "noserver", Message = "Could not connect to server", Data = "" };
    }
    

On the server-side I'm using

    public async Task<JsonApiResult> GetMyFileUploads([FromBody]JsonApiResult result)
    {
    ...
djack109
  • 1,261
  • 1
  • 23
  • 42
  • I find it weird that it's not throwing any exceptions, it shouldn't be able to,mind going through that function while dedugging on a step-by-step approach ? To see which line fails ? – Irwene Apr 23 '20 at 14:35
  • 1
    If you can avoid it don't send large data encapsulated as JSON, send it als plain raw data instead (working with large JSON data always requires a lot of RAM). Second use `GetStreamAsync` to avoid first reading the complete response into memory. Using the stream you can e.g. directly save the incoming data to a file. – Robert Apr 23 '20 at 14:35
  • @Sidewinder94 I have tried to step through it and the last thing it does is the line under `////` it pauses for a while then drops straight the `noserver` result line. None of the exceptions gets triggered. I'm using JSON as I send multiple items of related data at the same time. so I know how to process it on the server. @Robert note I am sending the data to a server. I only want the small response back – djack109 Apr 23 '20 at 14:48
  • OK,k I did a little more digging and found this on the line under `//<<<<`. I get an error `StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent` . It kinda looks like a buffer overrun. Like I said 25MB or more this happens, anything less is fine – djack109 Apr 23 '20 at 15:18

0 Answers0