c# - 插入到 Windows Azure 移动服务时出现 500 内部服务器错误?

标签 c# xaml azure azure-mobile-services

当使用从 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();
    }

一件可能很重要的事情是我能够将一个引脚插入数据库,但之后会抛出此错误。另外,如果我在 if(Pins.Count < 1) 行放置一个断点,则表明当数据库中有 pin 时 Pins.Count 返回 0。

此外,在 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();
}

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

更新1:我认为我的问题与http://social.msdn.microsoft.com/Forums/en-US/azuremobile/thread/82ae07a1-5832-4dc9-97d7-1cda1fb33bc2有关,但这个问题仍然没有答案。

最佳答案

我猜下面的行会产生问题

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

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

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

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

关于c# - 插入到 Windows Azure 移动服务时出现 500 内部服务器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12434533/

相关文章:

c# - 领域驱动设计和聚合

wpf - 覆盖 generic.xaml 中定义的样式

c# - Azure Functions - 无法从 Azure WebJobs SDK 调用

c# - 多核机器上的多线程没有最大化 CPU

c# - 锁定非线程安全的对象,这是可以接受的做法吗?

wpf - 如何使调整 WPF 窗口的大小减少 "laggy"?

c# - 未获得焦点时更改文本框的背景

c# - 如何捕获 Windows Azure(辅助)角色中未处理的异常

azure - Terraform azurerm azurerm_data_factory_dataset_binary 参数定义

c# - 在 Unity 中读取串口 "COM3"。连接建立成功,但未调用 DataReceived Handler