2

I'm working on a cross platform library that makes HTTP requests. It's working fine on Android, but when I try to use it on iOS I'm getting an exception and I can't figure out how to fix it.

Here is my code:

// method from cross platform library

Task.Factory.StartNew(delegate
{
    try
    {
        var client = new HttpClient();
        // some other setup stuff
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.post, "http://myurl.com...");

        var task = client.SendAsync(request);
        task.Wait(); // Exception thrown on this line

        var response = task.Result;

        var responseString = response.Content.ReadAsStringAsync().Result;
    }
    catch (Exception e)
    {
    }
}

On task.Wait(); I get a System.AggregateException with an inner exception of System.InvalidOperationException that says Operation is invalid due to the current state of the object.

Trying to find some solutions, I found that the issue could be cause by calling this on the UI thread. But that's the whole point of wrapping this all in Task.Factory.StartNew.

I've tried everything I know to do and have yet to solve the issue. Any help would be very appreciated.

Edit:

I decided to try my solution on an iPhone simulator. It's an iPhone 6 simulator running iOS 10. My physical device is the same. It works on the simulator, but not the physical device for some reason... very strange.

Edit 2:

Thanks to @YuriS for finding a solution.

From: https://forums.xamarin.com/discussion/36713/issue-with-microsoft-http-net-library-operation-is-not-valid-due-to-the-current-state-of-the-objec

What you can do is: 1) Go to References of ios Project 2) Edit References 3) Check 'System.Net.Http'

Behaviour for android is the same.

11
  • You can't guarantee that starting a new Task it is actually executed on a separate Threadpool thread, can you? Why are you even doing it this way? Commented Oct 25, 2016 at 16:06
  • @Cheesebaron How can I guarantee that this is on a separate threadpool thread then? Commented Oct 25, 2016 at 16:08
  • @JaredPrice I confused on why you are doing it this way also, but do you have the same Nuget packages installed on both your Android and iOS projects that you are using in your "cross platform library"? Commented Oct 25, 2016 at 16:13
  • @SushiHangover Yes. What are you confused about? Is there a better way to do this? Commented Oct 25, 2016 at 16:21
  • There is definitely better way doing this. I can provide it in answer if you want (let me know) BUT I cannot reproduce what you describe. I copied your code and it works on my phone and simulator. I call this function at button press. When do you call it? Commented Oct 25, 2016 at 17:09

1 Answer 1

2

There can be few problems described here: https://forums.xamarin.com/discussion/36713/issue-with-microsoft-http-net-library-operation-is-not-valid-due-to-the-current-state-of-the-objec

https://bugzilla.xamarin.com/show_bug.cgi?id=17936

"Operation is not valid" error at Xamarin.iOS project with HttpClient

http://motzcod.es/post/78863496592/portable-class-libraries-httpclient-so-happy

Seems all post pointing on System.Net.Http

Regardless of the problem there is a better ways doing this.One of them:

public static async Task PostRequest()
{
    try
    {
        HttpClient client = new HttpClient();
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://myuri");
        //request.Headers.Add("", "");
        var response = await client.SendAsync(request);
        var responseString = await response.Content.ReadAsStringAsync();
    }
    catch (Exception ex)
    {

    }
}

If you want to wait till function completes you call

await PostRequest();

If you don't need to wait then just omit "await" in the call or use

PostRequest.ContinueWith((t)=>
{
});

Also you need to handle an exception within the function, so probably returning just Task is not the best. I was just basing my answer on original function signature

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.