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.

Jared Price
  • 5,217
  • 7
  • 44
  • 74
  • 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? – Cheesebaron Oct 25 '16 at 16:06
  • @Cheesebaron How can I guarantee that this is on a separate threadpool thread then? – Jared Price Oct 25 '16 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"? – SushiHangover Oct 25 '16 at 16:13
  • @SushiHangover Yes. What are you confused about? Is there a better way to do this? – Jared Price Oct 25 '16 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? – Yuri S Oct 25 '16 at 17:09
  • @YuriS Yeah that would be appreciated. I'm willing to try whatever at this point. I'm calling the function on what I believe to be the UI thread, but I assumed that wrapping my functionality in `Task.Factory.StartNew` would prevent any issues related to calling on the UI thread. – Jared Price Oct 25 '16 at 17:17
  • @YuriS I tried it on a simulator as well and it works... For some reason it won't work on my iPhone, but it does work on the simulator. Very strange... Same device, same iOS version, yet it doesn't work on the physical device but works on the simulator... – Jared Price Oct 25 '16 at 17:44
  • Please read links I posted in my answer. They describe that situation when it works on simulator but not on device. Make sure you have updated all Nuget packages – Yuri S Oct 25 '16 at 17:46
  • @YuriS Wow... that did it. Thank you very much. – Jared Price Oct 25 '16 at 17:52
  • What did? Updating packages or including System.Net.Http – Yuri S Oct 25 '16 at 17:54
  • @YuriS Including System.Net.Http. – Jared Price Oct 25 '16 at 17:55

1 Answers1

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

Community
  • 1
  • 1
Yuri S
  • 5,355
  • 1
  • 15
  • 23