将异常传递给HttpHandler

时间:2010-12-10 18:45:09

标签: c# asp.net httphandler webclient

我试图通过执行以下操作将异常传递给HttpHandler:

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) { }
}

修改

我在client.UploadData()行上遇到以下异常。 “无法为不写入数据的操作设置Content-Length或Chunked Encoding。”

修改

即使我将我的调用更改为client.UploadString(位置,“这是测试!”);它仍然以相同的例外失败。

2 个答案:

答案 0 :(得分:0)

我敢打赌,因为你永远不会关闭你的流,你的数组长度为零。


试试这个:

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);
    }
}

答案 1 :(得分:0)

事实证明这是由.Net注册的AXD处理程序引起的。当我将扩展名更改为.axdx时,一切都开始工作了。