title: Throwing WCF Fault Exception from RealProxy of Service Contract
body: <p>I have a C# application with a WCF backend and a UWP frontend. I have a service implementing my service contract. Everything works as expected if I catch exceptions on the services and re-throw them as Fault Exceptions. </p>
  
  <p>Now I'm trying to clean up my service code by wrapping the service call using an instance of RealProxy and throwing my Fault Exceptions from the proxy. Unfortunately, now when an exception is thrown on the service, my client receives an exception with the following message:</p>
  
  <p>"The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error."</p>
  
  <p>I have the following Service Contract:</p>
  
  <pre><code>[ServiceContract(Namespace = @"blah/blah/blah", SessionModel = SessionMode.Required)
  public interface IService1
  {
      [OperationContract]
      [FaultContract(typeof(Exception))]
      bool DoSomething(Setting setting);
  }
  </code></pre>
  
  <p>With the following implementation:</p>
  
  <pre><code>[Export(typeof(IService1)]
  [ServiceBehavior(InstanceContextMode = IntanceContextMode.Single,
      ConcurrencyMode = ConcurrencyModel.Single]
  public class Service1 : IService1
  {
      bool DoSomthing(Setting setting)
      {
          try
          {
              throw new ArgumentException("setting");
          }
          catch (Exception ex)
          {
              throw WrapException(ex);
          }
      } 
  
      private FaultException&lt;Exception&gt; WrapException(Exception ex)
      {
          string message = exception.GetBaseException().Message;
          Console.WriteLine(string.Format("Exception: {0}", message));
          return new FaultException&lt;Exception&gt;(exception, new FaultReason(message));
      }
  }
  </code></pre>
  
  <p>My WCF service host has the following configuration with corresponding configuration on the client side.</p>
  
  <pre><code>&lt;system.serviceModel&gt;
      &lt;services&gt;
        &lt;service name="Host.Services.Service1"&gt;
          &lt;endpoint address="net.tcp://localhost:8082/Service1" 
              binding="netTcpBinding" 
              contract="Host.Domain.IService1"/&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;bindings&gt;
        &lt;netTcpBinding&gt;
          &lt;binding maxReceivedMessageSize="1000000"&gt;
            &lt;security mode="None"&gt;&lt;/security&gt;
          &lt;/binding&gt;
        &lt;/netTcpBinding&gt;
      &lt;/bindings&gt;
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="true"/&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
    &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>Now, I've created a proxy:</p>
  
  <pre><code>public class ServiceProxy&lt;T&gt; : RealProxy
      {
          private readonly T _instance;
  
          private ServiceProxy(T instance)
          : base(typeof(T))
          {
              _instance = instance;
          }
  
          public static T Create(T instance)
          {
              return (T) new ServiceProxy&lt;T&gt;(instance).GetTransparentProxy();
          }
  
          public override IMessage Invoke(IMessage msg)
          {
              var methodCall = (IMethodCallMessage) msg;
              var method = (MethodInfo) methodCall.MethodBase;
  
              try
              {
                  var result = method.Invoke(_instance, methodCall.InArgs);
                  return new ReturnMessage(result, null, 0, methodCall.LogicalCallContext, methodCall);
              }
              catch (Exception ex)
              {
                  string message = ex.GetBaseException().Message;
                  Console.WriteLine(string.Format("Exception: {0}", message));
                  var wrapped = new FaultException&lt;Exception&gt;(ex, new FaultReason(message));
  
                  return new ReturnMessage(wrapped, msg as IMethodCallMessage);
              }
          }
      }
  </code></pre>
  
  <p>(Thanks to @bradmo for the idea.)</p>
  
  <p><a href="https://stackoverflow.com/questions/15733900/dynamically-creating-a-proxy-class">Dynamically creating a proxy class</a></p>
  
  <p>I'm using the proxy as my service:</p>
  
  <pre><code>static void Main(string[] args)
  {
      try
      {
          var service = new Bootstrapper().Run().GetExport&lt;IService1&gt;().Value;
          IService1 proxy = ServiceProxy&lt;IService1&gt;.Create(service);
  
            using (var host = new ServiceHost(proxy))
            {
                  host.Open();
                  Console.WriteLine("Running...");
                  Console.ReadLine();
            }
       }
       catch (Exception ex)
       {
            Console.WriteLine(string.Format("Exception: {0}", ex.GetBaseException().Message));
            Console.ReadLine();
       }
    }
  }
  </code></pre>
  
  <p>Throwing Fault Exceptions without the proxy works fine and the proxy works fine when there are no exceptions but when I use the proxy to wrap an exception, I get the following error on the client side:</p>
  
  <p>"The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error."</p>