[英]ObjectDisposedException in xamarin HttpClient.PutAsync
I faced a problem with Xamarin's HttpClient
PutAsync
method in a simple application. 我在一个简单的应用程序中遇到了Xamarin的HttpClient
PutAsync
方法的问题。 It has a ListActivity
subclass with an OnListItemClick
method overriden. 它有一个ListActivity
子类,其OnListItemClick
方法覆盖。
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: MyClass
只包含一个方法:
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
. 我总是在httpClient.PutAsync
得到一个ObjectDisposedException
。 It says that System.Net.Sockets.Socket
was disposed. 它说System.Net.Sockets.Socket
被处理掉了。
But if I change PutAsync
to GetAsync
everything works just fine. 但是,如果我将PutAsync
更改为GetAsync
一切正常。
What am I doing wrong? 我究竟做错了什么?
I found the reason. 我找到了原因。 There was no controller registered to handle Toggle method on the web service. 没有注册控制器来处理Web服务上的Toggle方法。 When this was fixed I succeeded to get 200 OK. 当这个问题得到解决后,我成功地获得了200分。 But I still wonder why PutAsync gave me ObjectDisposedException instead of more appropriate Bad Request. 但我仍然想知道为什么PutAsync给了我ObjectDisposedException而不是更合适的Bad Request。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.