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

前端 未结 1 1591
孤城傲影
孤城傲影 2020-12-22 08:13

This code gives me the exception

Exception thrown: \'System.TypeLoadException\' in Unknown Module

public sealed class Sa         


        
相关标签:
1条回答
  • 2020-12-22 08:51

    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.

    0 讨论(0)
提交回复
热议问题