WCF client-side error-handling

后端 未结 3 699
粉色の甜心
粉色の甜心 2020-12-19 01:00

I\'m consuming a clunky WCF server that occasionally throws various exceptions, and additionally returns some of its errors as string. I have no access to the s

3条回答
  •  感动是毒
    2020-12-19 01:27

    I've ended up using something based on the answers in this question.

    It sticks to the generated client code, and allows invocation of the operations generically.

    The code is incomplete, feel free to fork and edit it. Please notify me if you found any bugs or made any updates.

    It's pretty bulky so I'll just share the usage code:

    using (var proxy = new ClientProxy())
    {
      client.Exception += (sender, eventArgs) =>
      {
        //All the exceptions will get here, can be customized by overriding ClientProxy.
        Console.WriteLine($@"A '{eventArgs.Exception.GetType()}' occurred 
          during operation '{eventArgs.Operation.Method.Name}'.");
        eventArgs.Handled = true;
      };
      client.Invoke(client.Client.MyOperation, "arg1", "arg2");
    }
    

提交回复
热议问题