问题描述
这段代码给了我例外
抛出异常:'System.TypeLoadException' in Unknown Module
public sealed class SampleBackgroundTask2 : IBackgroundTask
{
EasClientDeviceInformation currentDeviceInfo;
BackgroundTaskCancellationReason _cancelReason = BackgroundTaskCancellationReason.Abort;
BackgroundTaskDeferral _deferral = null;
IBackgroundTaskInstance _taskInstance = null;
ThreadPoolTimer _periodicTimer = null;
//
// The Run method is the entry point of a background task.
//
public 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();
_taskInstance = taskInstance;
_periodicTimer = ThreadPoolTimer.CreateTimer(new TimerElapsedHandler(PeriodicTimerCallbackAsync), TimeSpan.FromSeconds(1));
}
private async void PeriodicTimerCallbackAsync(ThreadPoolTimer timer)
{
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
{
_periodicTimer.Cancel();
_deferral.Complete();
}
}
private void OnCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
{
_cancelReason = reason;
}
}
如果我评论 async/await 和 HttpClient 地方,那么也不例外.
If I comment async/await and HttpClient places then there is no exception.
那么我的代码有什么问题?还是用UWP后台任务做异步GET/POST好?
So what's wrong with my code?Or Is it done well to use UWP Background Task to make async GET/POST?
我尝试了一些经典的解决方案,例如
I have tried some classic solution like
public async void Run(IBackgroundTaskInstance taskInstance)
{
BackgroundTaskDeferral _deferral = taskInstance.GetDeferral();
//
// Start one (or more) async
// Use the await keyword
//
// await SomeMethodAsync();
var uri = new System.Uri("http://www.bing.com");
using (var httpClient = new Windows.Web.Http.HttpClient())
{
// Always catch network exceptions for async methods
try
{
string result = await httpClient.GetStringAsync(uri);
}
catch (Exception ex)
{
// Details in ex.Message and ex.HResult.
}
}
_deferral.Complete();
}
但是一旦我把 HttpClient
放在 SomeMethodAsync()
里面,它就不能处理上面的错误了.
but once I put HttpClient
inside of SomeMethodAsync()
it does not work with the error above.
此解决方案没有帮助 HttpClient.GetAsync 在具有锁定屏幕访问权限和 TimeTrigger 或 MaintenanceTrigger 的后台任务中失败
谢谢!
推荐答案
我稍微简化了解决方案并删除了 ThreadPoolTimer
,因为我不确定为什么从代码中使用它.请说明解决方案是否需要.
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.
如果 ThreadPoolTimer
是可选的,那么您可以尝试以下代码:
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;
}
}
这篇关于抛出异常:UWP 后台任务中未知模块中的“System.TypeLoadException"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!