I'm having hard time trying to figure out how to properly handle exceptions within async functions. Having the following code:
static async Task NotifyPartyAsync(Uri uri, string paramName, string paramValue)
{
string link = string.Format("{0}?{1}={2}", uri, paramName, HttpUtility.UrlEncode(paramValue));
using (var client = new HttpClient())
try {
using (HttpResponseMessage response = await client.GetAsync(link, HttpCompletionOption.ResponseHeadersRead))
logger.DebugFormat("HTTP {0} from {1}", (int)response.StatusCode, link);
}
catch (Exception ex) {
logger.Error(ex);
}
}
static void NotifyParty(Uri uri, string paramName, string paramValue)
{
string link = string.Format("{0}?{1}={2}", uri, paramName, HttpUtility.UrlEncode(paramValue));
using (var client = new HttpClient())
try {
using (HttpResponseMessage response = client.GetAsync(link, HttpCompletionOption.ResponseHeadersRead).Result)
logger.DebugFormat("HTTP {0} from {1}", (int)response.StatusCode, link);
}
catch (Exception ex) {
logger.Error(ex);
}
}
How do I refactor it to not repeat 99% code common to both functions? I need to notify an uri both synchronously and asynchronously and I don't care about the result except I want it logged.