1

I am trying to pass an exception to an HttpHandler by doing the following:

catch (Exception e)
{
    byte[] exceptionData;

    MemoryStream stream = new MemoryStream();
    BinaryFormatter formatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Persistence));
    formatter.Serialize(stream, e);
    exceptionData = stream.ToArray();

    WebClient client = new WebClient();
    Uri handler = new Uri(ApplicationUri, "TransferException.axd");

    #if DEBUG
    ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(BypassAllCertificateStuff);
    #endif

    try
    {
        client.UploadData(handler, exceptionData);
    }
    catch (WebException) { }
}

EDIT

I am getting the following exception on the client.UploadData() line. "Content-Length or Chunked Encoding cannot be set for an operation that does not write data."

EDIT

Even if I change my call to be client.UploadString(location, "THIS IS A TEST!"); it still fails with the same exception.

Hungry Beast
  • 3,677
  • 7
  • 49
  • 75

2 Answers2

0

I bet that because you're never closing your stream, your array is of zero length.


Try this:

catch (Exception ex)
{
    byte[] exceptionData;

    using (MemoryStream stream = new MemoryStream())
    {
        BinaryFormatter formatter = new BinaryFormatter(
            null, new StreamingContext(StreamingContextStates.Persistence));
        formatter.Serialize(stream, ex);
        exceptionData = stream.ToArray();
    }

    using (WebClient client = new WebClient())
    {
        Uri handler = new Uri(ApplicationUri, "TransferException.axd");

#if DEBUG
        ServicePointManager.ServerCertificateValidationCallback +=
            new RemoteCertificateValidationCallback(BypassAllCertificateStuff);
#endif

        client.UploadData(handler, exceptionData);
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Nope, I copied and pasted this code and still received the same exception. It's odd, I can't find any references to this exception message on the web. – Hungry Beast Dec 10 '10 at 20:52
  • @Chris: my next step would be to confirm that `exceptionData.Length` is non-zero at the time of the `UploadData` call. Note also that the docs say "This member outputs trace information when you enable network tracing in your application. For more information, see [Network Tracing](http://msdn.microsoft.com/en-us/library/hyb3xww8.aspx)." I would also break in the debugger and confirm what's in the `Headers` property before the call. Finally, I'd comment out the certificate validation handler for now, just to simplify things. – John Saunders Dec 10 '10 at 21:40
  • That doesn't appear to be the issue because even if I change my .UploadData() call to client.UploadString(location, "THIS IS A TEST!"); it still fails. – Hungry Beast Dec 10 '10 at 21:46
  • @Chris: Ok, then I would definitely remove the `ServicePointManager` stuff, then wrap the call in a `try { ... } catch (Exception ex){Console.WriteLine(ex.ToString());}` then post the full exception here. Remove the try/catch when done debugging. – John Saunders Dec 10 '10 at 21:58
  • @alhambraeidos: I have no idea. That's what was in the original question. – John Saunders Dec 27 '10 at 21:29
  • @alhambraeidos: Take a look at this http://flimflan.com/blog/SafelyRunningBackgroundThreadsInASPNET20.aspx – Hungry Beast Jan 03 '11 at 23:53
  • 1
    @Chris: note that the article is about .NET 2.0, and there may be better solutions in later releases. – John Saunders Jan 04 '11 at 01:41
0

It turns out this was due to a .Net registered AXD handler. When I changed the extension to .axdx everything started working.

Hungry Beast
  • 3,677
  • 7
  • 49
  • 75