Best way to catch a WCF exception in Silverlight?

前端 未结 9 1054
悲哀的现实
悲哀的现实 2020-12-31 06:43

I have a Silverlight 2 application that is consuming a WCF service. As such, it uses asynchronous callbacks for all the calls to the methods of the service. If the service

相关标签:
9条回答
  • 2020-12-31 06:49

    With Silverlight 3 the the Visual Studio debugger catches these exceptions so that the exception handler - confusingly - is never reached. However, when running without the debugger, the exception handler is called as expected. I guess this is ok as long as one is aware of it. I admit i wasted a few hours trying to figure out how to drill into the inner workings of Silverlight/Wcf/Browser to get to my exception. Don't go there.

    0 讨论(0)
  • 2020-12-31 06:52

    With XNA on WP7, I found I had no choice but to manually add try/catch to the various async End*FunctionName*() methods; nothing else I tried would prevent application failure & shutdown when the server was unavailable. It's a real drag having to manually update this code when a service changes.

    I am surprised this isn't a bigger issue since there doesn't seem to be any other way to catch these exceptions on WP7 using XNA but I suppose this just says more about how many (==not many) people are trying to do this than anything else. If they would just make slsvcutil.exe generate sync methods we could easily catch these within our own worker thread, but unfortunately the generated code uses thread pool threads with no means to catch the exceptions.

    0 讨论(0)
  • 2020-12-31 06:57

    To Handle this situation use the TargetInvocationException. This will catch the Exception when the network is down or your service is unavailable.

    0 讨论(0)
  • 2020-12-31 07:03

    I check the Error property of the event args in the service method completed event handler. I haven't had issues with the event handler not being called. In the case where the server goes down, the call takes a few seconds then comes back with a ProtocolException in the Error property.

    Assuming you have tried this and your callback really never gets called, you might look into customizing the generated proxy class. See this article.

    0 讨论(0)
  • 2020-12-31 07:04

    OOpps....

    Sorry wrong answer from my side (well the MSFT guy didn't hit the write answer service callbacks are called on the same UI thread), the thing is

    More info:

    - In development even detaching from the debugger, this method is never reached. 
    - On the production environment yes.
    

    My guess something related with Visual Studio options and intercepting exceptions.

    More info, in this thread http://silverlight.net/forums/p/48613/186745.aspx#186745

    Quite interesting topic.

    0 讨论(0)
  • 2020-12-31 07:07

    You can forget about Application_UnhandledException on asyn client callbacks, reason why:

    Application_UnhandledException only exceptions fired on the UI thread can be caught by Application.UnhandledExceptions

    This means... not called at all for a WCF async call :-).

    Check detailed response from MSFT

    http://silverlight.net/forums/t/21828.aspx

    Hello, only exceptions fired on the UI thread can be caught by Application.UnhandledExceptions. It can't catch exceptions from other threads. You can try this to trouble shoot the issue: In Visual Studio, from the Debug menu, choose Exceptions. Then check "Common Language Runtime Exceptions". This will make the debugger stop whenever an exception is thrown. But note this may be quite annoying sometimes since even if an exception is already caught. You can use the CheckBoxes to filter the exceptions you want to catch.

    Good news in my case is that handling the error message just in the clietn service call back is enough if you are not debugging.

    Thanks

    Braulio
    
    0 讨论(0)
提交回复
热议问题