First, do not use .*Async() if you intend to call .Wait() immediately. This is bad practice and will most likely lead to erroneous results. Instead, use synchronous versions of calls, client.Get(relativeUri) and the following:
TResult value = response.Content.ReadAs<TResult>(); return value;
If you intend not to use the asynchronous programming model .NET frameworks.
But if you prefer to use the capabilities of asynchronous I / O, you should do this in accordance with best practices. Use async / await keywords and make your methods look like synchronization using the capabilities of asynchronous .NET keywords.
I can only imagine that the entry point to your method looks something like this:
public TResult InvokeClientGet<TResult>(string relativeUri) {
This actually prevents you from using the async / wait keywords. Instead, try the following:
public async Task<TResult> InvokeClientGet<TResult>(string relativeUri) { try { using (var client = new HttpClient { BaseAddress = _serviceBaseAddress }) { using (var response = await client.GetAsync(relativeUri)) { if (response.StatusCode == HttpStatusCode.NotFound || !response.IsSuccessStatusCode) { return default(TResult); } return await response.Content.ReadAsAsync<TResult>(); } } catch (Exception ex) {
A few words about System.Threading.ThreadAbortException .
When you call the Abort method to kill a thread, the common runtime throws a ThreadAbortException. ThreadAbortException is a special exception that can be caught, but it will be automatically raised again at the end of the catch block. When this exception occurs, the runtime executes all finally blocks until the thread ends. Because a thread can perform unlimited computation in finally blocks or call Thread.ResetAbort to cancel an interrupt, there is no guarantee that the thread will ever end. If you want to wait for the interrupted thread to finish, you can call the Thread.Join method. Join is a blocking call that does not return until the thread actually terminates.
With that said, if something calls .Abort() and raises this exception. There is not much to prevent this. Regardless, try to follow best practices.
David pine
source share