locked
How to handle FaultException from a WCF service call in a WinRT component

    Question

  • Hi,

    In Windows Store all calls to WCF are now async, which is logic as a WCF call take some time depending on the quality of the network.

    I have a service that can throw some Fault that are carried by the SOAP protocol and are thrown as FaultException on the client side. I have wrapped the service in a WinRT component and I use it in a Windows Store (Metro) app. When a call to the service throws a supported Fault which doesn't break the communication I would like to handle this Fault and take some action to continue with other calls on the service.

    Unfortunately when the FaulktException is thrown by the service call it is in the underlying thread of the async call and I don't know in a Windows store App how to intercept this exception. The result is that it ends the app, which is not very friendly!

    Both the WinRT component and the application are written in C#, but I know that the core of WinRT is COM and in COM an exception cannot cross the boundary of the component, only HRESULT! So eventually I would like to intercept this exception inside the WinRT component, but I don't know how to do that or if it even possible.

    Any hint appreciated!

    Thanks 
    O. Rouit


    Software Architect, Advanced IT Tokens Team - Gemalto

    Wednesday, January 30, 2013 7:56 AM

Answers

  • Hi orouit,

    Although the windows runtime components are like traditional COM objects (you can invoke them in c++, .NET or javascript apps), if you use .NET (C#) to create a custom windows runtime compoonent, you can just use .NET programming syntact to write the windows runtime classes (as long as) you follow the component syntax requirement:

    #Creating Windows Runtime Components in C# and Visual Basic
    http://msdn.microsoft.com/en-us/library/windows/apps/br230301(v=VS.85).aspx

    and for asynchronous operations like webservice call, HttpClient requests or file IO access, you can just use the TAP pattern (using xxxxAsync method and await keyword) to call async operations with synchronous coding syntax.

    #Asynchronous Programming with Async and Await (C# and Visual Basic)
    http://msdn.microsoft.com/en-us/library/hh191443.aspx

    #Async and Await in Windows Store Application
    http://www.c-sharpcorner.com/UploadFile/krishnasarala/async-and-await-in-windows-store-application/


    and since you want to catch potential FaultException in your webservice call, then you can just add "try...catch..." block around the webservice call (with "await" keyword). In case exception occurs, you can change to call another async operation as you want. Such kinds of code logic can be well encapsulated in your Windows Runtime class.

    In addition, to better understand your detailed problem scenario, would you share some code of the windows runtime class/method you have written so that we can take a look to see if that matches our understanding.

    BTW, since you're writing the WinRT component in C#, posting the question in Windows Store C# programming forum would be better:)


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Jesse Jiang Monday, February 4, 2013 2:57 AM
    • Marked as answer by Jesse Jiang Tuesday, February 5, 2013 9:24 AM
    Friday, February 1, 2013 8:53 AM

All replies

  • Hello,

     

    I will involve more experts to investigate it.

     

    Best regards,

    Jesse


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 1, 2013 8:34 AM
  • Hi orouit,

    Although the windows runtime components are like traditional COM objects (you can invoke them in c++, .NET or javascript apps), if you use .NET (C#) to create a custom windows runtime compoonent, you can just use .NET programming syntact to write the windows runtime classes (as long as) you follow the component syntax requirement:

    #Creating Windows Runtime Components in C# and Visual Basic
    http://msdn.microsoft.com/en-us/library/windows/apps/br230301(v=VS.85).aspx

    and for asynchronous operations like webservice call, HttpClient requests or file IO access, you can just use the TAP pattern (using xxxxAsync method and await keyword) to call async operations with synchronous coding syntax.

    #Asynchronous Programming with Async and Await (C# and Visual Basic)
    http://msdn.microsoft.com/en-us/library/hh191443.aspx

    #Async and Await in Windows Store Application
    http://www.c-sharpcorner.com/UploadFile/krishnasarala/async-and-await-in-windows-store-application/


    and since you want to catch potential FaultException in your webservice call, then you can just add "try...catch..." block around the webservice call (with "await" keyword). In case exception occurs, you can change to call another async operation as you want. Such kinds of code logic can be well encapsulated in your Windows Runtime class.

    In addition, to better understand your detailed problem scenario, would you share some code of the windows runtime class/method you have written so that we can take a look to see if that matches our understanding.

    BTW, since you're writing the WinRT component in C#, posting the question in Windows Store C# programming forum would be better:)


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by Jesse Jiang Monday, February 4, 2013 2:57 AM
    • Marked as answer by Jesse Jiang Tuesday, February 5, 2013 9:24 AM
    Friday, February 1, 2013 8:53 AM
  • I could use AggregateException around the call to the service method and then propagate a platform exception from the WinRT component to the application.

    That's solving my problem

     

    Software Architect, Advanced IT Tokens Team - Gemalto

    Saturday, February 9, 2013 8:22 AM