예제 #1
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            var deferral = taskInstance.GetDeferral();

            try
            {
                await Log.InfoAsync("Synchronization background task started");

                var authenticatedSilently = await SecurityManager.TryAuthenticateSilently();

                if (authenticatedSilently)
                {
                    IBackendServiceClient storage = new MobileServiceBackendServiceClient(new SyncHandler(), new EventManager(), new LocalSettingsService());
                    await storage.InitializeAsync();
                    await storage.TrySyncAsync();
                }
                else
                {
                    await Log.WarnAsync("Authentication failed.");
                }

                await Log.InfoAsync("Synchronization background task completed");
            }
            catch (Exception ex)
            {
                await ExceptionHandlingHelper.HandleNonFatalErrorAsync(ex, "Synchronization background task failed.", sendTelemetry: false);
            }
            finally
            {
                deferral.Complete();
            }
        }
예제 #2
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            var deferral = taskInstance.GetDeferral();

            try
            {
                await Log.InfoAsync("LogsUpload background task started");

                var logsUploadPending = Equals(ApplicationData.Current.LocalSettings.Values[LocalSettingsKeys.LogsUploadPending], true);

                if (!logsUploadPending)
                {
                    await Log.InfoAsync("There are no pending logs to upload.");
                    return;
                }

                var authenticatedSilently = await SecurityManager.TryAuthenticateSilently();

                if (authenticatedSilently)
                {
                    IBackendServiceClient storage = new MobileServiceBackendServiceClient(new SyncHandler(), new EventManager(), new LocalSettingsService());
                    await storage.InitializeAsync();

                    var logsUploadService = new LogSharingService(storage);

                    var uri = await logsUploadService.ShareCurrentLogAsync();

                    await Log.DebugAsync("Log is shared at: " + uri);

                    ApplicationData.Current.LocalSettings.Values[LocalSettingsKeys.LogsUploadPending] = false;
                }
                else
                {
                    await Log.WarnAsync("Authentication failed.");
                }

                await Log.InfoAsync("LogsUpload background task completed");
            }
            catch (NoInternetConnectionException)
            {
                await Log.InfoAsync("There is no internet connection. Do nothing.");
            }
            catch (Exception ex)
            {
                await ExceptionHandlingHelper.HandleNonFatalErrorAsync(ex, "LogsUpload background task failed.", sendTelemetry: false);
            }
            finally
            {
                deferral.Complete();
            }
        }