Exception thrown: 'System.TypeLoadException' in Unknown Module in UWP Background Task

只谈情不闲聊 提交于 2019-11-29 17:34:01

I simplified the solution a bit and removed the ThreadPoolTimer since I was not sure why it was being used from the code. Please mention if it is required for the solution.

If the ThreadPoolTimer is optional then you can try the following code :

public sealed class SampleBackgroundTask2 : IBackgroundTask
    {

        EasClientDeviceInformation currentDeviceInfo;

        BackgroundTaskCancellationReason _cancelReason = BackgroundTaskCancellationReason.Abort;

        BackgroundTaskDeferral _deferral = null;

        //
        // The Run method is the entry point of a background task.
        //
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            currentDeviceInfo = new EasClientDeviceInformation();

            var cost = BackgroundWorkCost.CurrentBackgroundWorkCost;
            var settings = ApplicationData.Current.LocalSettings;
            settings.Values["BackgroundWorkCost2"] = cost.ToString();

            taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);

            _deferral = taskInstance.GetDeferral();
            await asynchronousAPICall();
            _deferral.Complete(); //calling this only when the API call is complete and the toast notification is shown
        }
        private async Task asynchronousAPICall()
        {
            try
            {
                var httpClient = new HttpClient(new HttpClientHandler());

                string urlPath = (string)ApplicationData.Current.LocalSettings.Values["ServerIPAddress"] + "/Api/Version1/IsUpdatePersonal";

                HttpResponseMessage response = await httpClient.PostAsync(urlPath,
                    new StringContent(JsonConvert.SerializeObject(currentDeviceInfo.Id.ToString()), Encoding.UTF8, "application/json")); // new FormUrlEncodedContent(values)

                response.EnsureSuccessStatusCode();

                if (response.IsSuccessStatusCode)
                {
                    string jsonText = await response.Content.ReadAsStringAsync();
                    var customObj = JsonConvert.DeserializeObject<bool>(jsonText, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All });

                    if (customObj) // Если TRUE  то да надо сообщить пользователю о необходимости обновления
                    {
                        ShowToastNotification("Ttitle", "Message");
                    }
                }
            }
            catch (HttpRequestException ex)
            {
            }
            catch (Exception ex)
            {
            }
            finally
            {
                _deferral.Complete();
            }
        }

        private void OnCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
        {
            _cancelReason = reason;
        }
    }

Please let me know if this works for you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!