我们有一个应用程序使用HttpWebRequest类调用远程Web地址,我们通过WebRequest.Create
方法获取。
这是我们的实际代码:
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "HEAD";
request.Timeout = this.connectionTimeout;
if (this.usePipelinedConnection)
{
request.KeepAlive = true;
request.Pipelined = true;
}
request.BeginGetResponse(cb => logService.EndGetRequestStream(cb), null);
现在以不确定的方式(无法找到重现它的模式),我们得到以下错误:
System.InvalidCastException
无法将“System.Net.HttpWebResponse”类型的对象强制转换为“System.Exception”。
使用此堆栈跟踪:
at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult,TransportContext& context)
at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult)
at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
at System.Net.ContextAwareResult.CaptureOrComplete(ExecutionContext& cachedContext,Boolean returnContext)
在System.Net.ContextAwareResult.FinishPostingAsyncOp()
at System.Net.HttpWebRequest.BeginGetResponse(AsyncCallback callback,Object state)
有关此方法的文档报告了几个可以引发的异常,但InvalidCastException
不是其中之一,这意味着它在microsoft方法中未处理。
我开始挖掘.Net来源,我认为我找到了罪魁祸首。在HttpWebResponse.EndGetResponseStream方法中,有这一行:
throw (Exception) lazyAsyncResult.Result;
这是此方法中唯一存在Exception,因此必须如此。现在,该方法的实现方式是,只有在connectionstream为null时才会到达此行,因此lazyasyncresult.Result
属性应包含异常。但是在我的情况下,到达了分支但是lazyasyncresult.Result
包含HttpWebResponse
,因此装箱失败并且我得到了该错误。现在我有两个注意事项:
lazyasyncresult.Result
的内容是正确的,那么无论如何都会抛出(因为该行以抛出开始),但这将是一个有意义的错误; 现在我的问题很简单:我该如何防止这种情况发生?我在代码中做错了什么,或者这是MS方法中的一个普通错误?
以下,作为参考,该方法的MS源。我在有罪的路线上添加了一些评论。
感谢大家的时间。
public Stream EndGetRequestStream(IAsyncResult asyncResult, out TransportContext context)
{
if (Logging.On)
Logging.Enter(Logging.Web, (object) this, "EndGetRequestStream", "");
context = (TransportContext) null;
if (asyncResult == null)
throw new ArgumentNullException("asyncResult");
LazyAsyncResult lazyAsyncResult = asyncResult as LazyAsyncResult;
if (lazyAsyncResult == null || lazyAsyncResult.AsyncObject != this)
throw new ArgumentException(SR.GetString("net_io_invalidasyncresult"), "asyncResult");
if (lazyAsyncResult.EndCalled)
{
throw new InvalidOperationException(SR.GetString("net_io_invalidendcall", new object[1]
{
(object) "EndGetRequestStream"
}));
}
else
{
ConnectStream connectStream = lazyAsyncResult.InternalWaitForCompletion() as ConnectStream;
lazyAsyncResult.EndCalled = true;
if (connectStream == null)
{
if (Logging.On)
Logging.Exception(Logging.Web, (object) this, "EndGetRequestStream", lazyAsyncResult.Result as Exception);
// Here result contains HttpWebResponse so the cast to Exception fails.
// It would throw anyway (since there' a throw) but I think, since result contains a response
// that the code shouldn't be hitting this if branch.
throw (Exception) lazyAsyncResult.Result;
}
else
{
context = (TransportContext) new ConnectStreamContext(connectStream);
if (Logging.On)
Logging.Exit(Logging.Web, (object) this, "EndGetRequestStream", (object) connectStream);
return (Stream) connectStream;
}
}
}
答案 0 :(得分:3)
我没有使用过这个异步任务库,如果这是一个天真的问题,请原谅我。但是..你不是在调用错误的EndGetXXX函数吗?
request.BeginGetResponse(cb => logService.EndGetRequestStream(cb), null);
我不知道那个logService变量是什么,但它不应该调用EndGetResponse()而不是吗?