WCF web service call - which exception(s) to catch?

前端 未结 3 686
别跟我提以往
别跟我提以往 2021-01-12 20:49

I have a program that calls an external web service, and I want to present the user with a friendly dialog if e.g. the server is down, someone cut the cable etc. Assuming th

3条回答
  •  执念已碎
    2021-01-12 21:25

    It's not really the client's job to provide as much detail as possible. The maximum amount you really have to provide at the client side is as much as you get back in your exception.

    var userName = "bob";
    try 
    {      
       client.MyWebService(userName);
    }
    catch(Exception ex)
    {
       //Maybe we know WellKnownExceptions and can provide Foo advice:
       if (ex is WellKnownException)
       {
           Console.WriteLine("WellKnownException encountered, do Foo to fix Bar.");
       }
       //otherwise, this is the best you can do:
       Console.WriteLine(string.Format(
             "MyWebService call failed for {0}. Details: {1}", userName, ex));
    }
    

提交回复
热议问题