C# 引发异常:';System.TypeLoadException';UWP后台任务中的未知模块
这个代码给了我一个例外 引发异常:未知模块中的“System.TypeLoadException” 但是,一旦我将C# 引发异常:';System.TypeLoadException';UWP后台任务中的未知模块,c#,uwp,httpclient,background-task,C#,Uwp,Httpclient,Background Task,这个代码给了我一个例外 引发异常:未知模块中的“System.TypeLoadException” 但是,一旦我将HttpClient放入SomeMethodAsync()中,它就无法处理上述错误 这个解决方案没有帮助 谢谢 我稍微简化了解决方案,删除了ThreadPoolTimer,因为我不知道为什么要从代码中使用它。请说明解决方案是否需要此选项 如果ThreadPoolTimer是可选的,则可以尝试以下代码: public sealed class SampleBackgroundTask2
HttpClient
放入SomeMethodAsync()
中,它就无法处理上述错误
这个解决方案没有帮助
谢谢 我稍微简化了解决方案,删除了
ThreadPoolTimer
,因为我不知道为什么要从代码中使用它。请说明解决方案是否需要此选项
如果ThreadPoolTimer
是可选的,则可以尝试以下代码:
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;
}
}
公共密封类SampleBackgroundTask2:IBackgroundTask
{
EasClientDeviceInformation当前设备信息;
BackgroundTaskCancellationReason\u cancelReason=BackgroundTaskCancellationReason.Abort;
BackgroundTaskDeleral _deleral=null;
//
//Run方法是后台任务的入口点。
//
公共异步无效运行(IBackgroundTaskInstance taskInstance)
{
currentDeviceInfo=新的EasClientDeviceInformation();
var成本=背景工作成本。当前背景工作成本;
var settings=ApplicationData.Current.LocalSettings;
settings.Values[“BackgroundWorkCost2”]=cost.ToString();
taskInstance.Cancelled+=新背景任务CanceledEventHandler(OnCanceled);
_延迟=taskInstance.getDeleral();
等待asynchronousAPICall();
_deleral.Complete();//仅当API调用完成且显示toast通知时调用此函数
}
专用异步任务asynchronousAPICall()
{
尝试
{
var httpClient=newhttpclient(newhttpclienthandler());
字符串urlPath=(字符串)ApplicationData.Current.LocalSettings.Values[“ServerIPAddress”]+“/Api/Version1/IsUpdatePersonal”;
HttpResponseMessage response=等待httpClient.PostAsync(urlPath,
新建StringContent(JsonConvert.SerializeObject(currentDeviceInfo.Id.ToString()),Encoding.UTF8,“application/json”);//新建FormUrlEncodedContent(值)
response.EnsureSuccessStatusCode();
if(响应。IsSuccessStatusCode)
{
string jsonText=await response.Content.ReadAsStringAsync();
var customObj=JsonConvert.DeserializeObject(jsonText,新的JsonSerializerSettings(){TypeNameHandling=TypeNameHandling.All});
if(customObj)//Сааааааааааааааааа
{
ShowToastNotification(“Ttitle”、“Message”);
}
}
}
捕获(HttpRequestException-ex)
{
}
捕获(例外情况除外)
{
}
最后
{
_延迟。完成();
}
}
已取消私有void(IBackgroundTaskInstance发件人,BackgroundTaskCancellationReason)
{
_取消原因=原因;
}
}
请让我知道这是否适合你
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();
}
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;
}
}