I have a simple IPC mechanism that uses WCF and named pipes. My goal is to distribute information about the details of the exception (including the stack) to the client for logging purposes (the rest of the application registration is on the client).
If I use the following code, I can catch a FaultException <Exception> on the client and view the exception details:
Contract:
[ServiceContract] public interface IService { [OperationContract] [FaultContract(typeof(Exception))] void DoSomething(); }
Implementation:
public class Service : IService { public void DoSomething() { try { ThisWillThrowAnException(); } catch (Exception e) { throw new FaultException<Exception>(e); } } }
Client:
public void CallServer() { try { proxy.DoSomething(); } catch (FaultException<Exception> e) { Console.WriteLine("Caught fault exception!"); } }
This works great and I see a message printed on the console. However, if I want to use my own derived exception instead of the Exception base class, it fails.
Custom exception:
[Serializable] public class MyException : Exception { public MyException () { } public MyException (string message) : base(message) { } public MyException (string message, Exception inner) : base(message, inner) { } protected MyException ( SerializationInfo info, StreamingContext context) : base(info, context) { } }
Change FaultContract to IService.DoSomething to
typeof(MyException).
Change the throw clause in the service to
new FaultException<MyException>(new MyException(e.Message, e);
Change the catch clause on the client to
catch (FaultException<MyException> e)
When I do this, the CommunicationException error appears on the client: System.ServiceModel.CommunicationException: An error occurred while reading from the pipe: the pipe was finished. (109, 0x6d).
The MyException class is in the shared library, available to both the client and server.
This question is very similar to this question , but it did not help me.