Unhandled Exception when HttpClient executes PostAsync then Wait()

Animusz_Animusz_ PLMember ✭✭

Hello,

I have static method to get object from remote server via WebAPI.
When I run the application I am getting exception like below. For Android it works fine, but for iOS I got exception.

{System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or more errors occurred. ---> System.InvalidOperationException: Operation is not valid due to the current state of the object.
at System.Lightup.Call[TI,TV] (System.Delegate& storage, TI instance, System.String methodName, TV parameter) [0x0000d] in <1f06874135f14e4da7d57d9ad60bf90d>:0
at System.Lightup.Set[TI,TV] (System.Delegate& storage, TI instance, System.String propertyName, TV value) [0x00000] in <1f06874135f14e4da7d57d9ad60bf90d>:0
at System.Net.HttpWebRequestLightup.SetContentLength (System.Net.HttpWebRequest instance, System.Int64 value) [0x00000] in <1f06874135f14e4da7d57d9ad60bf90d>:0
at System.Net.Http.HttpWebRequest.set_ContentLength (System.Int64 value) [0x00000] in <1f06874135f14e4da7d57d9ad60bf90d>:0
at System.Net.Http.HttpClientHandler.PrepareAndStartContentUpload (System.Net.Http.HttpClientHandler+RequestState state) [0x000d1] in <1f06874135f14e4da7d57d9ad60bf90d>:0
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00013] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.2/src/mono/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs:2159
at System.Threading.Tasks.Task1[TResult].GetResultCore (System.Boolean waitCompletionNotification) [0x0002b] in <bdb3aaa208384edf9f1266721cf8f9c1>:0 at System.Threading.Tasks.Task1[TResult].get_Result () [0x0000f] in :0
at Helper.ApiHelper.UserProfileHelper+<>c__DisplayClass1_0.b__0 (System.Threading.Tasks.Task1[TResult] taskwithresponse) [0x00001] in UserProfileHelper.cs:56 at System.Threading.Tasks.ContinuationTaskFromResultTask1[TAntecedentResult].InnerInvoke () [0x00024] in :0
at System.Threading.Tasks.Task.Execute () [0x00010] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.2/src/mono/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs:2502
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00013] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.2/src/mono/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs:2159
at System.Threading.Tasks.Task.Wait (System.Int32 millisecondsTimeout, System.Threading.CancellationToken cancellationToken) [0x00043] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.2/src/mono/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs:3189
at System.Threading.Tasks.Task.Wait () [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.2/src/mono/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs:3054
at Helper.ApiHelper.UserProfileHelper.ReceiveUserProfile (Model.ApiModel.PowerUp powerUp) [0x0004c] in UserProfileHelper.cs:64
---> (Inner Exception #0) System.AggregateException: One or more errors occurred. ---> System.InvalidOperationException: Operation is not valid due to the current state of the object.
at System.Lightup.Call[TI,TV] (System.Delegate& storage, TI instance, System.String methodName, TV parameter) [0x0000d] in <1f06874135f14e4da7d57d9ad60bf90d>:0
at System.Lightup.Set[TI,TV] (System.Delegate& storage, TI instance, System.String propertyName, TV value) [0x00000] in <1f06874135f14e4da7d57d9ad60bf90d>:0
at System.Net.HttpWebRequestLightup.SetContentLength (System.Net.HttpWebRequest instance, System.Int64 value) [0x00000] in <1f06874135f14e4da7d57d9ad60bf90d>:0
at System.Net.Http.HttpWebRequest.set_ContentLength (System.Int64 value) [0x00000] in <1f06874135f14e4da7d57d9ad60bf90d>:0
at System.Net.Http.HttpClientHandler.PrepareAndStartContentUpload (System.Net.Http.HttpClientHandler+RequestState state) [0x000d1] in <1f06874135f14e4da7d57d9ad60bf90d>:0
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00013] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.2/src/mono/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs:2159
at System.Threading.Tasks.Task1[TResult].GetResultCore (System.Boolean waitCompletionNotification) [0x0002b] in <bdb3aaa208384edf9f1266721cf8f9c1>:0 at System.Threading.Tasks.Task1[TResult].get_Result () [0x0000f] in :0
at Helper.ApiHelper.UserProfileHelper+<>c__DisplayClass1_0.b__0 (System.Threading.Tasks.Task1[TResult] taskwithresponse) [0x00001] in UserProfileHelper.cs:56 at System.Threading.Tasks.ContinuationTaskFromResultTask1[TAntecedentResult].InnerInvoke () [0x00024] in :0
at System.Threading.Tasks.Task.Execute () [0x00010] in /Library/Frameworks/Xamarin.iOS.framework/Versions/11.6.1.2/src/mono/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs:2502
---> (Inner Exception #0) System.InvalidOperationException: Operation is not valid due to the current state of the object.
at System.Lightup.Call[TI,TV] (System.Delegate& storage, TI instance, System.String methodName, TV parameter) [0x0000d] in <1f06874135f14e4da7d57d9ad60bf90d>:0
at System.Lightup.Set[TI,TV] (System.Delegate& storage, TI instance, System.String propertyName, TV value) [0x00000] in <1f06874135f14e4da7d57d9ad60bf90d>:0
at System.Net.HttpWebRequestLightup.SetContentLength (System.Net.HttpWebRequest instance, System.Int64 value) [0x00000] in <1f06874135f14e4da7d57d9ad60bf90d>:0
at System.Net.Http.HttpWebRequest.set_ContentLength (System.Int64 value) [0x00000] in <1f06874135f14e4da7d57d9ad60bf90d>:0
at System.Net.Http.HttpClientHandler.PrepareAndStartContentUpload (System.Net.Http.HttpClientHandler+RequestState state) [0x000d1] in <1f06874135f14e4da7d57d9ad60bf90d>:0 <---
<---
}

Code looks like:

    public static UserProfile ReceiveUserProfile(this PowerUp powerUp)
    {
        HttpClient httpClient = new HttpClient();

        string jsonData = JsonConvert.SerializeObject(powerUp);

        StringContent stringContent = new StringContent(jsonData, Encoding.UTF8, ConfigHelper.WebServiceContentType);

        UserProfile receivedUserProfile = null;

        try
        {
            var task = httpClient.PostAsync(ConfigHelper.ReceiveUserProfileLink, stringContent).ContinueWith((taskwithresponse) =>
            {
                var response = taskwithresponse.Result;
                var jsonString = response.Content.ReadAsStringAsync();
                jsonString.Wait();
                receivedUserProfile = JsonConvert.DeserializeObject<UserProfile>(jsonString.Result);
                receivedUserProfile = receivedUserProfile?.DeviceId == null ? null : receivedUserProfile;
            });

            task.Wait();
        }
        catch (Exception e)
        {
            SessionController.IsOffline = true;
            return null;
        }

        return receivedUserProfile;
    }

Best Answer

Answers

  • Animusz_Animusz_ PLMember ✭✭

    Hi JGoldberger,

    Thanks for sample. It works - but before I needed add the System.Net.Http library to reference just.

    Best regards,
    Patrick

Sign In or Register to comment.