【问题标题】:App sudden got a Timeout exception and Internal server error应用突然出现超时异常和内部服务器错误
【发布时间】:2018-03-13 12:35:45
【问题描述】:

我的 xamarin 应用程序上的注册和登录功能突然停止工作。它有时会给出:

未处理的异常:Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException:发生

其他时间:

未处理的异常:Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException:无法完成请求。 (内部服务器错误)发生

我没有更改这些页面上的任何内容来影响它们。唯一发生的事情是我的 azure 订阅过期了,我更新了它。除非我缺少某些设置,否则连接仍然相同。

我确定这两个页面上的问题是相同的,所以我将在此处显示我的注册页面。如果您需要查看我的登录信息,请告诉我。

编辑 1:我添加了一个 try/catch,它总是会被捕获。

编辑 2:应用程序可能在其主线程上做了太多工作。

这出现在日志中,不知道为什么它突然不断给出这些异常,我尝试没有任何改善。

编辑 3:尝试干净构建、卸载、重新安装,但现在我不知所措。到今天为止它运行良好,现在我遇到了上面提到的这些问题,但我似乎没有任何办法解决它。

        EditText Username = FindViewById<EditText>(Resource.Id.txtUsername);
        EditText Password = FindViewById<EditText>(Resource.Id.txtPassword);
        EditText ConfirmPassword = FindViewById<EditText>(Resource.Id.txtConfirmPassword);
        if (Password.Text == ConfirmPassword.Text)
        {
            try
            {
                string hashedPassword = PasswordStorage.CreateHash(Password.Text);
                string userName = Username.Text.Trim();
                users newU = new users { username = userName, password = hashedPassword };
                List<users> allUsers = await Client.GetTable<users>().ToListAsync();
                Toast.MakeText(this, "Awaits table", ToastLength.Short).Show();
                users u = allUsers.FirstOrDefault(x => x.username == newU.username);
                if (u == null)
                {
                    DBHelper.InsertNewUser(newU);
                    Toast.MakeText(this, "User " + newU.username + " created! You can now log in!", ToastLength.Short).Show();
                    StartActivity(typeof(MainActivity));
                }
                else
                {
                    Toast.MakeText(this, "User " + u.username + " already exists!", ToastLength.Short).Show();
                }
            }
            catch (Exception)
            {
                Toast.MakeText(this, "Time out", ToastLength.Short).Show();
            }

        }

【问题讨论】:

  • 对于移动应用程序,您必须假设到服务器的连接可能在任何给定时间都无法正常工作。在设备上使用分布式数据库非常糟糕,这可能是必要的。总的来说,这些 Excpetitions 在本质上听起来很简单。您应该处理的罕见情况。但我不能 100% 排除他们不是 Boneheaded 而不是。这里有两篇关于正确异常处理的文章,我链接了很多。 blogs.msdn.com/b/ericlippert/archive/2008/09/10/… | codeproject.com/Articles/9538/…

标签: c# azure xamarin azure-mobile-services


【解决方案1】:

对于您的代码,您可以针对在线表优化查询,如下所示:

var client = new MobileServiceClient("https://<your-app-name>.azurewebsites.net");
var users = await client.GetTable<users>().Where(u=>u.username == newU.username).ToListAsync();
if(users.Any())
{
   //user exists
}
else
{
   //add new user
}

对于您的情况,我建议您Enable diagnostics logging 来捕获详细的错误。对于 C# 后端,您可以在 Startup.MobileApp.cs 文件下指定 config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;,然后您可以在响应中检索详细的错误。并且你需要调试你的代码并使用fiddler来捕获网络痕迹,错误可能是Client.GetTable&lt;users&gt;().ToListAsync()DBHelper.InsertNewUser(newU)抛出的,所以你需要自己排除故障。如果您无法解决此问题,您可以使用您尝试过的更详细的步骤和检索到的详细请求跟踪来更新问题,以便我们缩小此问题的范围。

另外,你可以关注阿德里安·霍尔关于The Mobile Client的书。

【讨论】:

  • 我现在已经修复了,我还对您显示的查询进行了优化,但不断收到“无法转换泛型类型列表”。我添加了 VS 建议的“公共静态隐式运算符”,并带有“throw new NotImplementedException();”在我的 getter 和 setter 类中。现在它一直抛出那个异常。我在这里很愚蠢,错过了一些东西,有什么想法吗?
  • 我刚刚定义了一个简单的users 类。对于您的新问题,您可以更新您的问题,或者您最好使用详细信息(模型定义等)创建一个新问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-16
  • 1970-01-01
  • 2020-06-27
  • 1970-01-01
  • 2015-05-29
  • 2021-10-07
相关资源
最近更新 更多