【问题标题】:MobileServiceClient MobileServiceInvalidOperationException Response Content is nullMobileServiceClient MobileServiceInvalidOperationException 响应内容为空
【发布时间】:2017-07-28 16:22:12
【问题描述】:

我在 Xamarin Forms 应用中使用以下代码:

HttpResponseMessage response = null;

try
{
    HttpContent content = new StringContent(JsonConvert.SerializeObject(register), Encoding.UTF8, "application/json");

    response = await client.InvokeApiAsync("register", content, HttpMethod.Post, null, null);

    if (!response.IsSuccessStatusCode)
    {
        string error = await response.Content.ReadAsStringAsync();

        var def = new { Message = "" };
        var errorMessage = JsonConvert.DeserializeAnonymousType(error, def);

        return KloverResult.BuildError(true, errorMessage.Message);
    }
}
catch (MobileServiceInvalidOperationException e)
{
    if (e.Response.StatusCode == System.Net.HttpStatusCode.InternalServerError)
    {
        string error = await e.Response.Content.ReadAsStringAsync();

        var def = new { Message = "" };
        var errorMessage = JsonConvert.DeserializeAnonymousType(error, def);

        return KloverResult.BuildError(true, errorMessage.Message);
    }
    else
    {
        return KloverResult.BuildError(false, "Invalid username or password");
    }
}

我遇到的问题是由于 500 引发 MobileServiceInvalidOperationException。当我尝试读取响应的内容 (e.Response.Content) 时,它为空。当我使用 Restlet 调用相同的 API 时,我得到以下响应:

{
"Message": "Name jblogs is already taken."
}

这是我期望在我的错误变量中的值,但它为空。

我的问题是,我应该能够阅读响应的内容吗?如果是这样,我是否需要在客户端/服务器上进行更多设置?被调用的 API 使用以下方式从 webapi 返回错误:

Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Name jblogs is already taken.");

任何帮助将不胜感激。

【问题讨论】:

    标签: xamarin.forms asp.net-web-api2 azure-mobile-services mobileserviceclient


    【解决方案1】:

    500 响应意味着服务器崩溃。在那种情况下,很可能没有内容。

    如果您的 API 返回 status=500,那么它做错了事。您应该做的是返回 400 系列中的状态 - 409(冲突)对我来说似乎合适。

    如果您的 API 没有故意返回 status=500,则服务器崩溃并且您无法获取内容。

    【讨论】:

    • 服务器故意返回 500 - 将尝试您对 400 系列错误的建议,并查看客户端如何处理该错误
    • 使用 409 时响应的内容仍然为空
    【解决方案2】:

    根据您的描述,我使用自定义 WebApi 端点构建了我的移动应用应用程序来测试此问题。根据我的测试,我利用Microsoft.Azure.Mobile.Client 3.1.0 调用自定义WebApi,当响应状态为409 或500 时,我可以通过Response.Content.ReadAsStringAsync() 检索内容等等。这是我的代码sn-p,你可以参考一下:

    WebApi

    [MobileAppController]
    public class ValuesController : ApiController
    {
        public async Task<HttpResponseMessage> Get()
        {
            await Task.Delay(TimeSpan.FromSeconds(2));
            return Request.CreateErrorResponse(HttpStatusCode.Conflict, "Name jblogs is already taken.");
        }
    }
    

    客户端应用

    try
    {
        MobileServiceClient client = new MobileServiceClient("https://bruce-chen-002.azurewebsites.net/");
        var response = await client.InvokeApiAsync("/api/values", HttpMethod.Get, null);
    }
    catch (MobileServiceInvalidOperationException e)
    {
        if (e.Response.StatusCode == System.Net.HttpStatusCode.InternalServerError)
        {
            string error = await e.Response.Content.ReadAsStringAsync();
        }
    }
    

    结果

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      • 2010-09-28
      • 1970-01-01
      相关资源
      最近更新 更多