0

I am working on a Xamarin cross platform project that talks to a web service. The general form of my service calls follows the following pattern:

var clientQuery = new MyClientsQuery(param1, param2);
var json = JsonConvert.SerializeObject(clientQuery);
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpClient _httpClient = new HttpClient();
response = await _httpClient.PostAsync(ServiceURL, content);
if (response.IsSuccessStatusCode)
{
    json = await response.Content.ReadAsStringAsync();
}

This usually works fine, but after making a few calls during the execution of the app, PostAsync generates the exception:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index

I suspect that I am running into some sort of connection limit, but I can not find any documentation in System.Net.Http that indicates when I should be disposing of objects or any other clue.

Why is this happening?

10
  • That exception is likely coming from the webservice you are calling. Are you sure you are providing valid parameters? Commented Jan 13, 2017 at 18:57
  • Yes, the parameters are valid. Commented Jan 13, 2017 at 19:16
  • Well, to my knowledge, PostAsync will not throw an ArugmentOutOfRangeException . You may be seeing the inner exception returned by the web service you are calling Commented Jan 13, 2017 at 19:23
  • Can you post a full stack trace for that exception? Commented Jan 13, 2017 at 19:32
  • The "full" stack trace really isn't all that full. This is all that shows when the exception is raised 0xB in UIKit.UIApplication.Main at /Users/builder/data/lanes/3969/7beaef43/source/xamarin-macios/src/UIKit/UIApplication.cs:79,4 C# 0x3B in UIKit.UIApplication.Main at /Users/builder/data/lanes/3969/7beaef43/source/xamarin-macios/src/UIKit/UIApplication.cs:63,4 C# > 0x8 in CSOSA.iOS.Application.Main at C:\Code\CsoApp\CSOSA\CSOSA.iOS\Main.cs:17,13 C# Commented Jan 13, 2017 at 19:46

1 Answer 1

1

I think you are right with your connection limit. The way I am solving this is to end the previous client connections I have. Once I did that, I stopped getting this error.

For others getting this error, see if you have a lot of other http_clients you have created and end the connections if you can.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.