【问题标题】:How to consume service api with applicationKey in Xamarin forms?如何在 Xamarin 表单中使用带有 applicationKey 的服务 api?
【发布时间】:2017-09-28 11:39:21
【问题描述】:

我是 Windows 应用程序开发的新手。我正在尝试使用具有以下代码块的移动服务客户端使用服务 api

 MobileServiceClient mSClient;
 mSClient = new MobileServiceClient("https://*******.azure-mobile.net/", "UFakeKkrayuAeVnosdfghrwvnhdfVAcjYziYFSFHUV89");

 Dictionary<string, string> userobj = new Dictionary<string, string>();
 userobj.Add("UserName", "username");
 userobj.Add("Password", "password");
 userobj.Add("DeviceUID", "010092780200134E0300215F0400CC37050083D807006B9B080066010900267F");
 userobj.Add("DeviceToken", "device_123");
 userobj.Add("OSVersion", "WindowsPhone");
 userobj.Add("AppVersion", "2.0");
 userobj.Add("DeviceModel", "Windows phone");
 userobj.Add("DeviceTypeID", "1");
 userobj.Add("DeviceAlertsOn","true");      

 var responce = mSClient.InvokeApiAsync("user/loguserin", HttpMethod.Post, userobj);

但我没有得到适当的回应:

谁能帮我解决这个问题?

谢谢, 斯里尼瓦斯。

【问题讨论】:

  • 调用异步方法时需要使用await
  • 添加到 jason 和 sushi,当你不使用 await 时,你的响应将包含任务对象。您可以从 responce 对象访问状态和错误。然后您可以稍后执行await responce 来运行它。这有助于更多的调试
  • 谢谢 Neville,能否请您给我一些适当的代码块以获得响应
  • 你写的方式很好。您可以在调试模式下检查对象。只需在该行下方添加await responce 行。我经常这样做是为了测试异步函数

标签: c# xamarin asp.net-web-api


【解决方案1】:

您没有等待异步方法,因此您看到的是WaitingForActivition的状态:

var responce= await mSClient.InvokeApiAsync(.......

【讨论】:

  • 谢谢 Sushi 的回复,我尝试了你的回答,现在我得到以下异常 mscorlib.ni.dll 中出现“Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException”类型的异常但未处理在用户代码中
  • @srinivaschalla 您需要查看异常 (MobileServiceInvalidO‌​perationException) 的 ValueResponse 属性以确定服务器拒绝您的呼叫的原因。 Provides additional details of an invalid operation specific to a Mobile Service. docs.microsoft.com/en-us/dotnet/api/…
  • 获取响应为 {StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Server: Microsoft-IIS/8.0 Set-Cookie :ARRAffinity=05b093af5f4e7c6d198da2b2771de80c714f0499531fcab7ac6cc84e48dfeafc;Path=/;HttpOnly;Domain=chiefservices.azure-mobile.net 日期:2017 年 9 月 27 日,星期三 05:08:30 GMT X-Powered-By:ASP.NET 内容长度:8 :应用程序/json; charset=utf-8 }} 和 Value as null
  • @srinivaschalla 在您的代码中,您发送的是字符串“用户名”和“密码”而不是这些变量的值,否则您需要发布另一个问题,其中包含失败的详细信息和详细信息user/loguserin 对服务器端的期望。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-15
  • 2017-05-03
相关资源
最近更新 更多