I am facing the following problem
My solution is setup using MVVM like this.
- A UWP app -> View
- A PCL targeting Windows Universal and .NET Framework 4.6 -> ViewModel
- A PCL targeting Windows Universal and .NET Framework 4.6 -> Model
In the View Project I have a service that makes web calls using Windows.Web.Http.HttpClient
. this service is injected into several ViewModels.
In a view model I have a try catch block like this.
try
{
await webClientService.MakeACallAsync();
}
catch (System.Runtime.InteropServices.COMException e)
{
//web exceptions such as 404, 401, 500 etc
// code handling exceptions.
}
The MakeACallAsync is this function, but the problem is not limited to this call only, it happens with other calls like it as well.
public async Task<string> MakeACallAsync(string username, string password)
{
var usernamepassword = $"{username}:{password}";
var bytes = Encoding.UTF8.GetBytes(usernamepassword);
var base64 = Convert.ToBase64String(bytes);
httpClient.DefaultRequestHeaders.Authorization = new HttpCredentialsHeaderValue("Basic", base64);
return await httpClient.GetStringAsync( a uri here );
}
Now in case the service throws any exceptions because of a 404, 500, etc response I catch these exceptions and handle them.
This works well in Debug, but in Release the exceptions are not thrown as System.Runtime.InteropServices.COMException
but as System.Exception
.
Does anybody know why that happens??