Passing an Exception to an HttpHandler
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 sti开发者_C百科ll fails with the same exception.
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);
}
}
It turns out this was due to a .Net registered AXD handler. When I changed the extension to .axdx everything started working.
精彩评论