我试图使用以下法典,但在出现例外情况时,它并不发挥作用。 谁能帮助我这样做? i 试图将网络例外放在鱼群中。 有可能将分散的数据类型退回(而不仅仅是说明)。
IObservable<string> tempReturnData = null;
try
{
// create http web request
HttpWebRequest WSrequest = (HttpWebRequest)WebRequest.Create(WebURL);
// instantiate Request class object
RequestState rs = new RequestState();
rs.Request = WSrequest;
// Lock current webrequest object.. incase of retry attempt
lock (WSrequest)
{
rs.Request.ContentType = "application/x-www-form-urlencoded";
rs.Request.Method = "POST";
rs.Request.Timeout = 100;
// bug in .net that closes the connection prior to it being finished
rs.Request.KeepAlive = false;
rs.Request.ProtocolVersion = HttpVersion.Version10;
// async pattern get request
var fetchRequestStream = Observable.FromAsyncPattern<Stream>(rs.Request.BeginGetRequestStream, rs.Request.EndGetRequestStream);
// async pattern get response
var fetchResponse = Observable.FromAsyncPattern<WebResponse>(rs.Request.BeginGetResponse, rs.Request.EndGetResponse);
//
tempReturnData = (from tempResult in fetchRequestStream() select tempResult).SelectMany(stream =>
{
using (var writer = new StreamWriter(stream)) writer.Write(postData);
// here i wants to catch web exception in fetchResponse() FYI : in my function i am returning IObservable<string>
return fetchResponse().Catch(Observable.Empty<WebResponse>()).Retry(5);
}).Select(result =>
{
lock (rs)
{
rs.Response = (HttpWebResponse)result;
string s = "";
// if response is ok then read response stream data
if (rs.Response.StatusCode == HttpStatusCode.OK)
{
using (StreamReader reader = new StreamReader(rs.Response.GetResponseStream())) s = reader.ReadToEnd();
}
// Error case if error occurs then try after random time period
else
{
if (Attempt < appConfig.PSPRequestAttempt)
{
Attempt++;
RandomisePost(WebURL, postData, Attempt);
}
}
return s;
}
}); // get response stream data
return tempReturnData;
}
}
catch (Exception ex)
{
// Debug.WriteLine("Exception Occurs " + ex.Message);
return null;
}