title: Azure Service Fabric remoting framework exception handling
body: <p>I have an Azure Service Fabric stateless service that a client connects to using remoting via ServiceProxy and I am trying to catch exceptions raised in the service within the client. The online documentation says:</p>
  
  <blockquote>
    <p>The remoting framework propagates exceptions thrown at the service to
    the client. So exception-handling logic at the client by using
    ServiceProxy can directly handle exceptions that the service throws.</p>
  </blockquote>
  
  <p>However, all I seem to get is a System.Exception with some other error message inside such as:</p>
  
  <blockquote>
    <p>Exception of type
    'ExceptionServiceCommon.Exceptions.MyCustomException' was thrown.</p>
  </blockquote>
  
  <p>I am using a seriously back-to-the-bone implementation in order to solve this for a problem in a more complex case.</p>
  
  <p>The service code is:</p>
  
  <pre><code>internal sealed class ExceptionService : StatelessService, IException
  {
      public ExceptionService(StatelessServiceContext context)
          : base(context)
      { }
  
      public Task ThrowException()
      {
          throw new MyCustomException();
      }
  
      protected override IEnumerable&lt;ServiceInstanceListener&gt; CreateServiceInstanceListeners()
      {
          return new[]
          {
              new ServiceInstanceListener(initParams =&gt; this.CreateServiceRemotingListener(initParams))
          };
      }
  }
  </code></pre>
  
  <p>The interface and exception defined in a common assembly are:</p>
  
  <pre><code>public interface IException : IService
  {
      Task ThrowException();
  }
  
  [Serializable]
  public class MyCustomException : Exception
  {
      public MyCustomException()
      { }
  
      public MyCustomException(string message)
          : base(message) { }
  
      public MyCustomException(string message, Exception innerException)
          : base(message, innerException)
      { }
  
      protected MyCustomException(SerializationInfo info, StreamingContext context) 
          : base(info, context)
      {
      }
  }
  </code></pre>
  
  <p>The code in the client (console application) is:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          var client = ServiceProxy.Create&lt;IException&gt;(new Uri("fabric:/ExceptionApplication/ExceptionService"));
  
          try
          {
              client.ThrowException().GetAwaiter().GetResult();
  
          }
          catch (MyCustomException)
          {
              Console.WriteLine("My Custom Exception");
          }
          catch (Exception)
          {
              Console.WriteLine("Plain old Exception");
          }
  
          Console.Read();
      }
  }
  </code></pre>
  
  <p>MyCustomException never gets caught, only Exception.</p>