我在web api项目中有一个单独的解决方案中的Web服务。我试图从Xamarin项目访问该Web服务。我正在使用我的Android手机进行调试,但是我收到了这个错误。我遇到的所有文章都是访问Web服务,使用模拟器。是因为这个我得到这个错误还是我错过了什么?
更新1:
我的代码如下,错误发生在第10行(包含using (WebResponse response = await request.GetResponseAsync())
的行)。
private async Task<JsonValue> GetNamesAsync()
{
try
{
string url = "http://{network-ip-address}:{port}/api/Names";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
request.ContentType = "application/json";
request.Method = "GET";
using (WebResponse response = await request.GetResponseAsync())
{
//Some code here
}
return null;
}
catch(Exception ex)
{
return null;
}
}
更新2:
如果我Step Into
(F11),则在大约2次点击后,将打开以下对话框。
答案 0 :(得分:1)
您正试图进入代码:
WebResponse response = await request.GetResponseAsync()
也就是说,您正在尝试调试GetResponseAsync()
类中的HttpWebRequest
方法。此类的源代码尚未加载到您的项目中(仅编译为.dll
),因此您无法进入此行。
尝试F10
跳过这一行。
您现在可能会收到相同的错误 - 但是,这次原因会有所不同。由于等待GetResponseAsync()
方法,程序流程将返回到调用GetNamesAsync()
的方法。如果等待该调用,并且对该方法的调用链等待所有等待回Activity
方法(因为不应该阻止UI),则要执行的下一行代码将是一行Xamarin / Android源代码中的代码,您再也无法访问。
e.g。
1 class MyActivity : Activity
2 {
3 // This function is called by the Xamarin/Android systems and is not awaited.
4 // As it is marked as async, any awaited calls within will pause this function,
5 // and the application will continue with the function that called this function,
6 // returning to this function when the awaited call finishes.
7 // This means the UI is not blocked and is responsive to the user.
8 public async void OnCreate()
9 {
10 base.OnCreate();
11 await initialiseAsync(); // awaited - so will return to calling function
12 // while waiting for operation to complete.
13
14 // Code here will run after initialiseAsync() has finished.
15 }
16 public async Task initialiseAsync()
17 {
18 JsonValue names = await GetNamesAsync(); // awaited - so will return to Line 11
19 // while waiting for operation to complete.
20
21 // Code here will run after GetNamesAsync() has finished.
22 }
23 }