title: Using an interface as an out parameter in WCF web service
body: <p>My situation is this.  On every call to my web service, I have an out parameter which is an error object.  If there is no error, the object indicates so by being empty. If there is an error then different properties are populated such as a "HasError" field, "ErrorMessage", "PrettyMessage", etc.  What I'm trying to do with this now is create different types of Error object which all implement an Error interface which I've defined already.  I then want to be able to have the out parameter be something like "out IMyError error" and then be able to set that error object either to one of my implementations of that interface depending on what kind of error I get in my method.  The problem I'm getting is that serialization doesn't seem to like this.  The method runs fine, but I don't get any data back on the client end.  Here is some code to hopefully clarify.</p>
  
  <p>My interface</p>
  
  <pre><code>public interface IMyError
  {
      bool HasError { get; set; }
      string ErrorType { get; set; }
      string PrettyErrMsg { get; set; }
  }
  </code></pre>
  
  <p>An example class implementation</p>
  
  <pre><code>[Serializable]
  [DataContract]
  public class AspError : IMyError
  {
      public AspError(Exception exception)
      {
          this.HasError = true;
          this.PrettyErrMsg = "An ASP Exception was thrown";
          this.ExceptionMsg = exception.Message;
          this.StackTrace = exception.StackTrace;
      }
  
      [DataMember(Name = "has_error", IsRequired = true)]
      public bool HasError { get; set; }
  
      [DataMember(Name = "error_type", IsRequired = true)]
      public string ErrorType
      {
          get
          {
              return "ASP";
          }
          set
          {
          }
      }
  
      [DataMember(Name = "pretty_error", IsRequired = true)]
      public string PrettyErrMsg { get; set; }
  
      [DataMember(Name = "exception", IsRequired = true)]
      public string ExceptionMsg { get; set; }
  
      [DataMember(Name = "stack_trace", IsRequired = true)]
      public string StackTrace { get; set; }
  }
  </code></pre>
  
  <p>And the method in my WCF service</p>
  
  <pre><code>public bool MyMethod(out IMyError error)
  {
      error = new MyError() { HasError = false };
  
      try
      {
          // do some code            
      }
      catch (Exception exception)
      {
          error = new AspError(exception);
          return false;
      }
  }
  </code></pre>
  
  <p>What I want is for that method, when an exception is caught, to return a AspError formatted as json the way it has worked in the past before I tried doing it as an interface.  Or if an different implemented class of IMyError occurs then return THAT type formatted as json.  I assumed it could work since they are both IMyError classes.</p>