다음을 통해 공유


Errors Without Context

I don't know how many people use the code I post, but I frequently stumble across peculiar or interesting behavior while trying to get the snippets working. If the behavior relates to the topic I'm trying to explain, then it goes in the article. If the behavior is something off to the side, then I'll frequently plan to write a future article and end up never getting around to it. Today's article is actually from the IErrorHandler that I showed yesterday although it has nothing at all to do with the problem of changing HTTP status codes.

Let's look at that error handler again with the addition of some debugging statements that I was trying to use.

 class HttpErrorHandler : IErrorHandler
{
   public bool HandleError(Exception error)
   {
      Console.WriteLine(OperationContext.Current.IncomingMessageHeaders.To);
      return false;
   }

   public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
   {
      Console.WriteLine(OperationContext.Current.IncomingMessageHeaders.To);
      if (fault != null)
      {
         HttpResponseMessageProperty properties = new HttpResponseMessageProperty();
         properties.StatusCode = HttpStatusCode.PaymentRequired;
         fault.Properties.Add(HttpResponseMessageProperty.Name, properties);
      }
   }
}

Adding these debugging statements turns out to cause the service to fail in an unexpected way, which I thought was pretty interesting. I was only using ProvideFault last time, but I put the statements in both HandleError and ProvideFault. If you'll recall, ProvideFault was being used to modify the message fault as it was being generated. HandleError controls whether the exception at fault should result in the affected state being torn down. I return false from HandleError because this error handler definitely does nothing to handle the exception. The whole point of the example was to have things fail after all.

If you run this code though, the debugging statement in ProvideFault works fine but the same statement in HandleError results in an ObjectDisposedException. By poking around, it turns out that between the time ProvideFault is called and HandleError is called, parts of the current message are already starting to go away. ProvideFault can safely use the OperationContext but that's not true for HandleError.

Is there any practical implication of this behavior? Well, if your ability to handle errors depends upon some information in the message, then you're going to have a problem when it comes to HandleError. I don't know of a pleasant way to solve this. The unpleasant way to solve this is to read all of the information you need during ProvideFault and stash it somewhere so that you can get to it from HandleError. I expect you could use some extensible objects (I'll talk about those in a few weeks) or you could staple the information onto the exception that's being passed around. Neither alternative sounds very good.

Next time: Disabling Security Timestamps

Comments

  • Anonymous
    January 25, 2007
    Let's pick up where we left off last time with the question… How do I modify the HTTP status code that