3

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??

Corcus
  • 1,070
  • 7
  • 25
  • Please explain the downvote and suggest a way to improve the question. – Corcus Aug 06 '16 at 14:51
  • 1
    We cannot see the code for `webClientService.MakeACallAsync()`. Trying to answer a question without access to all relevant information is going to result in guesswork. Not a convincing reason to downvote, to me, but others may feel different. – IInspectable Aug 06 '16 at 16:02
  • edited the question to provide the code for MakeACallAsync – Corcus Aug 07 '16 at 08:14
  • How did you get the Exception `System.Exception`? – Elvis Xia - MSFT Aug 08 '16 at 12:49
  • @ElvisXia-MSFT First I got some crashes from HockeyApp where I had uploaded the release version for testing with descriptions like Reason: Internal server error (500). Response status code does not indicate success: 500 (Internal Server Error). And thought "well I was handling those" Then I placed some breakpoints while running the app in relase mode and saw the problem. – Corcus Aug 08 '16 at 13:52
  • Could you please post the exception messages of `System.Exception` and `System.Runtime.InteropServices.COMException`? – Elvis Xia - MSFT Aug 10 '16 at 06:22
  • @ElvisXia-MSFT I'm experiencing the same issue. The exceptions are wrapped exceptions internally thrown by a web client when the connection to server is lost etc. I'm targeting Android and iOS and I can't find a way to make `COMException` visible in my project at all. – Shimmy Weitzhandler Mar 26 '17 at 06:49
  • @Corcus - Were you able to resolve this issue? In addition to the System.Exception firing, we are also getting the following: `System.ArgumentNullException in System.Private.CoreLib.dll`. Despite us handling the System.Exception and the method safely exiting, the ArgumentNullException seems to propogate further up the call stack, causing other methods to fail with unhandled exceptions. – nico van vuuren Apr 19 '17 at 12:29
  • @nicovanvuuren No unfortunately I haven't figured this out. I haven't experienced the ArgumentNullException though. – Corcus Apr 19 '17 at 15:48

0 Answers0