问题
My question is pretty simple : Can we use MobileServices in BackgroundTasks? (WP 8.1 XAML)
I have an universal app connected to a MobileService from Azure and I wanted to let the BackgroundTask synchronize the data of my remote database.
So, is this possible?
public sealed class BackgroundTask : IBackgroundTask
{
public async void Run(IBackgroundTaskInstance taskInstance)
{
BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
//Connect to mobile service and add data
deferral.Complete();
}
}
I can't add a reference to my WP project, so how can I get the App.MobileServiceClient? My BackgroundTask project is a Windows runtime component.
EDIT:
I've added the reference to Windows Azure Mobile Service by managing nuGet Packages and I can now declare a mobileService but when I want to instanciate it I've got the following error :
BACKGROUNDTASKHOST.EXE' has exited with code 1 (0x1).
So this is what my code looks like now :
public sealed class BackgroundTask : IBackgroundTask
{
public async void Run(IBackgroundTaskInstance taskInstance)
{
BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
Microsoft.WindowsAzure.MobileServices.MobileServiceClient lolServiceMobileClient =
new Microsoft.WindowsAzure.MobileServices.MobileServiceClient(
"https://myservicemobile.azure-mobile.net/",
"myKey");
deferral.Complete();
}
}
And this is the reference I've added :
Edit 2 I'm stills searching for solution and I'm now wondering, could it be because I exceed the amount of memory the background task is allowed to use?
回答1:
I'm not an expert on background tasks, but you can indeed use the Mobile Services SDK there. Just make sure that the background task doesn't interfere with the app, by using an app setting to lock between the two processes.
来源:https://stackoverflow.com/questions/27730408/azure-mobileservice-in-backgroundtask