I am trying to call a web service using RestSharp (this has to be done for WP8 onwards).
This is my triggered method:
private async void postRest()
{
string getSyncService = "MyService";
var client = new RestClient(ip);
var request = new RestRequest(getSyncService, Method.POST);
request.RequestFormat = DataFormat.Json;
JsonObject jsonGenericRequest = new JsonObject();
jsonGenericRequest.Add("companyid", "123");
jsonGenericRequest.Add("token", "123"); ...
request.AddParameter("GenMobileRequest", jsonGenericRequest);
request.AddHeader("Access-Control-Allow-Methods", "POST");
request.AddHeader("Content-Type", "application/json; charset=utf-8");
request.AddHeader("Accept", "application/json");
try
{
// easy async support
client.ExecuteAsync(request, response =>
{
Console.WriteLine("response content: " + response.Content);
if (response.ResponseStatus == ResponseStatus.Completed)
{
MessageBox.Show("errorMsg: " + response.ErrorMessage);
}
});
}
catch (System.Net.WebException ex)
{
MessageBox.Show(" " + ex.InnerException.ToString());
}
}
On my log I am getting this exception:
An exception of type 'System.Net.WebException' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary
I cant even keep any information in my handler
// Code to execute on Unhandled Exceptions
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
Console.WriteLine(" ---Application_UnhandledException---");
if (Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
Debugger.Break();
}
}
How can I get more info about what is wrong?
Any additional information on the proper way to call a WS would be appreciated.
Thanks
Reason is that exceptions from an Async Void Method Can't Be Caught with Catch.
Async void methods have different error-handling semantics. When an exception is thrown out of an async Task or async Task method, that exception is captured and placed on the Task object. With async void methods, there is no Task object, so any exceptions thrown out of an async void method will be raised directly on the SynchronizationContext that was active when the async void method started
The error is that the async void method needs to be changed to an async Task method instead.
source: https://blogs.msdn.microsoft.com/ptorr/2014/12/10/async-exceptions-in-c/
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace AsyncAndExceptions
{
class Program
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += (s, e) => Log("*** Crash! ***", "UnhandledException");
TaskScheduler.UnobservedTaskException += (s, e) => Log("*** Crash! ***", "UnobservedTaskException");
RunTests();
// Let async tasks complete...
Thread.Sleep(500);
GC.Collect(3, GCCollectionMode.Forced, true);
}
private static async Task RunTests()
{
try
{
// crash
// _1_VoidNoWait();
// crash
// _2_AsyncVoidAwait();
// OK
// _3_AsyncVoidAwaitWithTry();
// crash - no await
// _4_TaskNoWait();
// crash - no await
// _5_TaskAwait();
// OK
// await _4_TaskNoWait();
// OK
// await _5_TaskAwait();
}
catch (Exception ex) { Log("Exception handled OK"); }
// crash - no try
// await _4_TaskNoWait();
// crash - no try
// await _5_TaskAwait();
}
// Unsafe
static void _1_VoidNoWait()
{
ThrowAsync();
}
// Unsafe
static async void _2_AsyncVoidAwait()
{
await ThrowAsync();
}
// Safe
static async void _3_AsyncVoidAwaitWithTry()
{
try { await ThrowAsync(); }
catch (Exception ex) { Log("Exception handled OK"); }
}
// Safe only if caller uses await (or Result) inside a try
static Task _4_TaskNoWait()
{
return ThrowAsync();
}
// Safe only if caller uses await (or Result) inside a try
static async Task _5_TaskAwait()
{
await ThrowAsync();
}
// Helper that sets an exception asnychronously
static Task ThrowAsync()
{
TaskCompletionSource tcs = new TaskCompletionSource();
ThreadPool.QueueUserWorkItem(_ => tcs.SetException(new Exception("ThrowAsync")));
return tcs.Task;
}
internal static void Log(string message, [CallerMemberName] string caller = "")
{
Console.WriteLine("{0}: {1}", caller, message);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.