500 Internal Server Error while inserting to Windows Azure Mobile Services?
-
02-07-2021 - |
Frage
When using the following code modified from the Azure for Mobile Services website, I get a "Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException was unhandledMessage: Internal Server Error (500 InternalServerError - Details: {"code":500,"error":"Internal Server Error"})" error.
My code:
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();
}
One thing that could be important is that I was able to insert one pin into the database, but after that this error is being thrown. Also, if I put a breakpoint at the if(Pins.Count < 1) line, it shows that Pins.Count is returning 0 when there is a pin in the database.
Also, in the Azure Management Portal, I have the following as my script for Insert (from 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();
}
and my script for read:
function read(query, user, request) {
query.where({ userId: user.userId });
request.execute();
}
The error is being thrown on the "await pinTable.InsertAsync(pin);" line within Refresh(). There are no error logs in my Azure management portal. If there's any other information you need, just ask :)
Update 1: I think my problem is related to http://social.msdn.microsoft.com/Forums/en-US/azuremobile/thread/82ae07a1-5832-4dc9-97d7-1cda1fb33bc2, but that question is still unanswered.
Lösung
I guess below line is creating problem
Pins = pinTable.Where(pin => true).ToCollectionView();
ToCollectionView();
always returns count as 0. This is the bug here. What do you think Josh Twist?
Stavros: I think below line can help your program to running smooth
List<Pin> Pins = await pinTable.Where(pin => true).ToListAsync();