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.

4
  • Side point: all the IDisposable objects there should be in using statements Dec 10, 2010 at 18:56
  • What line throws that content-length related exception? Dec 10, 2010 at 18:56
  • Sorry, the client.UploadData() throws the exception. Dec 10, 2010 at 19:14
  • what is the code of transferexception.axd ?
    – Kiquenet
    Dec 30, 2010 at 21:20

2 Answers 2

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);
    }
}
7
  • 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. Dec 10, 2010 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." 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. Dec 10, 2010 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. Dec 10, 2010 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. Dec 10, 2010 at 21:58
  • 1
    @Chris: note that the article is about .NET 2.0, and there may be better solutions in later releases. Jan 4, 2011 at 1:41
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.