我有点疑惑为什么我的尝试...捕获异常似乎无法正常工作。我设置了以下代码,它可以正常工作 - 调用WCF的restful服务。我决定通过关闭WCF restful服务来测试它,希望它会捕获错误消息或忽略200(ok)消息等但由于某种原因.. try catch和response.EnsureStatusIsSuccessful()似乎被忽略因此生成try catch中间的HttpStageProcessingException错误..
据我所知,当忽略200时,我只需要捕获ArgumentOutOfRangeException ...想法?
// set up passing parameters
var p = new HttpQueryString();
p.Add("username", username);
p.Add("password", password);
try
{
WebConfigurations cfg = new WebConfigurations();
HttpResponseMessage response;
// cfg - gets the base address for the URL of the WCF
using (HttpClient client = new HttpClient(cfg.GetBaseUrl()))
{
// exception HttpStageProcessingException occurs here...
response = client.Get(new Uri(client.BaseAddress + "MainService/ValidateUser"), p);
response.EnsureStatusIsSuccessful();
string memberallowed = string.Empty;
// process the resonse xml etc..NOT a problem...
ProcessStreamData proc = new ProcessStreamData();
memberallowed = proc.ReadStreamData(response);
// blah balh
return result;
}
}
catch (ArgumentOutOfRangeException aorEx)
{
Console.Write("Argument Out of Range: " + aorEx.Message);
return false;
}
// This really should not be here??? No error message is generated, too...
catch (HttpStageProcessingException timeEx)
{
Console.Write("Stage Processing" + timeEx.Message);
return false;
}
catch (Exception ex)
{
Console.Write("Standard error" + ex.Message);
return false;
}
答案 0 :(得分:1)
在告诉Visual Studio不要破坏抛出的异常之后,您是否尝试过这样做?从菜单,调试 - >例外,然后取消选中。
此外,您可能希望从Rest Starter Kit转储该版本的HttpClient。最新版本可以在http://nuget.org/List/Packages/HttpClient找到,并警告有重大变化。