【问题标题】:500 Internal Server Error while inserting to Windows Azure Mobile Services?插入到 Windows Azure 移动服务时出现 500 内部服务器错误?
【发布时间】:2012-09-15 04:35:10
【问题描述】:

使用从 Azure for Mobile Services 网站修改的以下代码时,我收到“Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException was unhandledMessage: Internal Server Error (500 InternalServerError - Details: {"code":500,"error" :"内部服务器错误"})" 错误。

我的代码:

    private MobileServiceCollectionView<pin> Pins;
    private IMobileServiceTable<pin> pinTable = App.MobileService.GetTable<pin>();
    public class pin
    {
        public string name { get; set; }
        public long timestamp { get; set; }
        public string type { get; set; }
        public object content { get; set; }
        public string category { get; set; }
        public string comments { get; set; }
        public int Id { get; set; }
    }
    private LiveConnectSession session;
    private async System.Threading.Tasks.Task Authenticate()
    {
        //authentication code from the Azure site, I deleted it here to save space.
    }
    public MainPage()
    {
        this.InitializeComponent();
    }
    public long getTimestamp()
    {
        //Find unix timestamp (seconds since 01/01/1970)
        long ticks = DateTime.UtcNow.Ticks - DateTime.Parse("01/01/1970 00:00:00").Ticks;
        ticks /= 10000000; //Convert windows ticks to seconds
        return ticks;
    }
    public async Task<bool> Refresh()
    {
        List<string> CategoriesMixed = new List<string>();
        Pins = pinTable.Where(pin => true).ToCollectionView();
        if (Pins.Count < 1)
        {
            pin pin = new pin();
            pin.category = "Welcome";
            pin.content = "Hello, World!";
            pin.name = "No Pins :(";
            pin.comments = string.Empty;
            pin.timestamp = getTimestamp();
            pin.type = "text";
            await pinTable.InsertAsync(pin);
        }
        else
        {
            foreach (pin nowPin in Pins)
            {
                await pinTable.DeleteAsync(nowPin); //since I'm still trying to get the inserting pins thing to work, I'm having it delete all pins it finds for the user.
            }
        }
        return true;
    }
    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.  The Parameter
    /// property is typically used to configure the page.</param>
    protected override async void OnNavigatedTo(NavigationEventArgs e)
    {
        await Authenticate();
        await Refresh();
    }

可能很重要的一件事是,我能够将一个 pin 插入数据库,但之后会引发此错误。另外,如果我在 if(Pins.Count

另外,在 Azure 管理门户中,我将以下内容作为插入脚本(来自 http://www.windowsazure.com/en-us/develop/mobile/tutorials/authorize-users-in-scripts-dotnet/):

function insert(item, user, request) {
  item.userId = user.userId;    
  request.execute();
}

还有我的阅读脚本:

function read(query, user, request) {
   query.where({ userId: user.userId });    
   request.execute();
}

错误正在“等待 pinTable.InsertAsync(pin);”上引发Refresh() 中的行。我的 Azure 管理门户中没有错误日志。如果您需要任何其他信息,请询问:)

更新 1:我认为我的问题与 http://social.msdn.microsoft.com/Forums/en-US/azuremobile/thread/82ae07a1-5832-4dc9-97d7-1cda1fb33bc2 有关,但该问题仍未得到解答。

【问题讨论】:

  • 当您在客户端收到 500 InternalServerError 时,您可以转到 Azure 中移动服务的管理控制台并检查日志以获取有关在服务器端引发的异常的更多详细信息。我知道这已解决,但此信息可能会帮助其他人查看此问题。

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


【解决方案1】:

我猜下面一行是在制造问题

Pins = pinTable.Where(pin => true).ToCollectionView(); 

ToCollectionView(); 总是返回计数为 0。这是这里的错误。你觉得乔什·特威斯特怎么样?

Stavros:我认为下面一行可以帮助您的程序顺利运行

List<Pin> Pins = await pinTable.Where(pin => true).ToListAsync(); 

【讨论】:

  • 我最终只是重做了所有代码并且它工作了:) 但我认为这会有所帮助,所以谢谢 :D
猜你喜欢
  • 1970-01-01
  • 2016-09-14
  • 1970-01-01
  • 2015-10-08
  • 2018-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-05
相关资源
最近更新 更多