简体   繁体   中英

ObjectDisposedException in xamarin HttpClient.PutAsync

I faced a problem with Xamarin's HttpClient PutAsync method in a simple application. It has a ListActivity subclass with an OnListItemClick method overriden.

protected override async void OnListItemClick(ListView l, View v, int position, long id)
{
    var item = this.itemList[position];

    var core = new MyClass();
    await core.Toggle(item);
}

MyClass contains only one method:

internal async Task Toggle(string id)
{
    using (var httpClient = new HttpClient())
    {
        var content = new StringContent("test");

        var url = $"http://192.168.1.35/api/Toggle/{id}"

        using (var result = await httpClient.PutAsync(url, content))
        {
            result.EnsureSuccessStatusCode();
        }
    }
}

I always get an ObjectDisposedException in httpClient.PutAsync . It says that System.Net.Sockets.Socket was disposed.

But if I change PutAsync to GetAsync everything works just fine.

What am I doing wrong?

I found the reason. There was no controller registered to handle Toggle method on the web service. When this was fixed I succeeded to get 200 OK. But I still wonder why PutAsync gave me ObjectDisposedException instead of more appropriate Bad Request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM