Forum Xamarin.Forms

Mono errors when connecting to an API

I am connecting to an API from my Xamarin Forms app to get data. I am seeing a lot of errors in the form:-

ObjectDisposedException
Cannot access a disposed object.
Object name: 'SslStream'.

and

ObjectDisposedException
Cannot access a disposed object.
Object name: 'MobileAuthenticatedStream'.

A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. (Cannot access a disposed object.
Object name: 'MobileAuthenticatedStream'.)

Does anyone have experience of what could be causing this issue?

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    For question:

    A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread.

    Usually, this problem arises when you don't handle task well.

    I couldn't see the detail of you code, but you can first check the following thread:

    https://stackoverflow.com/questions/7883052/a-tasks-exceptions-were-not-observed-either-by-waiting-on-the-task-or-accessi

  • grimordegrimorde Member ✭✭
    edited March 11

    The offending code (I believe) is:-

    public async Task<TReturn> Post<T, TReturn>(string url, T data)
    {
        try
        {
            HttpClient httpClient = CreateHttpClient(url);
    
            var content = new StringContent(JsonConvert.SerializeObject(data));
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    
            HttpResponseMessage response = null;
            response = await httpClient.PostAsync(url, content);
    
            if (response.IsSuccessStatusCode)
            {
                var result = JsonConvert.DeserializeObject<TReturn>(response.Content.ReadAsStringAsync().Result);
                return result;
            }
    
            return default(TReturn);
        }
        catch (HttpRequestException hex)
        {
            return default(TReturn);
        }
        catch (Exception ex)
        {
            throw;
        }
    }
    

    which is called throughout the app e.g.:-

    var result = await this._requestProvider.Post<JobRequest, ApiResponse<IEnumerable<JobDto>>>(ApiUrls.GetJobs,
                                jobRequest);
    

    which is wrapped in a try/catch to log the exception.

    The issue seems to occur when running with a slow network connection. I've replicated on an emulator by changing the network speeds, but I'm not sure why it's falling into the TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException; I've set up under MainActivity.cs rather than falling into the try/catch error handling around the call.

    The app is likely to be used in situations where there is a bad network connection. I already check for no connection and mistakenly thought the current code allowed us to handle bad connections.

    Any suggestions on how I can improve my process to accommodate a slow/bad network?

Sign In or Register to comment.