我有这个异步函数,可以从我的Xamarin.iOS应用程序中同时从两个不同的地方调用它。但是只有其中一个有效。
public async Task<Response> GetMenu(int appType, int appVersion)
{
try {
var url = string.Format(RestApiPaths.LEFT_MENU_CONTROLLER_LIST, appType, appVersion);
string result = await httpCall.GetAsync(url, App.Instance.Platform);
System.Diagnostics.Debug.WriteLine("GetMenu.Response: " + result);
return new Response(JsonConvert.DeserializeObject<List<LeftMenuDetails>>(result));
} catch (Exception e) {
Debug.WriteLine("---------------");
Debug.WriteLine(e.ToString());
return new Response(status: false, message: "", error: true, errorType: 500, errorMessage: "");
}
}
,对于另一个,结果为null。
应用程序输出
Cannot access a disposed object.
Object name: 'System.Net.Http.ByteArrayContent'.
GetLeftMenuÍ.Response:
Value cannot be null.
Parameter name: value
当我通过添加一些延迟来调用第二个时,两者都起作用。但是为什么它不能同时工作。
这是GetAsync方法:
public async Task<string> GetAsync(string url, int platform)
{
try {
using (var client = GetAuthenticatedHttpClient(platform)) {
using (var r = await client.GetAsync(new Uri(url))) {
string result = await r.Content.ReadAsStringAsync();
Debug.WriteLine(string.Format("Get({0}).Result: {1}", url, result));
if (string.Compare(result, NoSession) == 0 || string.Compare(result, NoSession2) == 0) {
bool ReLoginStatus = await userApiCall.ReLogin(platform, username, password);
if (!ReLoginStatus)
return null;
return await GetAsync(url, platform);
}
return result;
}
}
} catch (Exception e) {
Debug.WriteLine("-------------");
Debug.WriteLine(e.ToString());
}
return null;
}
编辑:
-------------
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.Net.Http.ByteArrayContent'.
at System.Net.Http.HttpContent.LoadIntoBufferAsync (System.Int64 maxBufferSize) [0x00019] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.Net.Http/System.Net.Http/HttpContent.cs:140
at System.Net.Http.HttpContent.ReadAsStringAsync () [0x00028] in /Users/builder/jenkins/workspace/xamarin-macios/xamarin-macios/external/mono/mcs/class/System.Net.Http/System.Net.Http/HttpContent.cs:172
at Gogch.HttpCall.GetAsync (System.String url, System.Int32 platform) [0x00104] in /Users/sarath/Developer/Workspace/GCH Mobile App/Gogch/APICalls/Implementation/HttpCall.cs:28
GetLeftMenu.Response:
---------------
System.ArgumentNullException: Value cannot be null.
Parameter name: value
at Newtonsoft.Json.Utilities.ValidationUtils.ArgumentNotNull (System.Object value, System.String parameterName) [0x00003] in <9b6398b0b3ae4afdb586fd62eda2ead4>:0
at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <9b6398b0b3ae4afdb586fd62eda2ead4>:0
at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <9b6398b0b3ae4afdb586fd62eda2ead4>:0
at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value) [0x00000] in <9b6398b0b3ae4afdb586fd62eda2ead4>:0
at Gogch.AccountHttpCall.GetMenu (System.Int32 appType, System.Int32 appVersion) [0x000f5] in /Users/sarath/Developer/Workspace/GCH Mobile App/Gogch/APICalls/Implementation/AccountHttpCall.cs:501