.net MVC RESTFUL HTTPClient try-catch-odity

.net MVC RESTFUL HTTPClient try-catch-odity,.net,wcf,model-view-controller,rest,httpclient,.net,Wcf,Model View Controller,Rest,Httpclient,我有点困惑为什么我的try…catch异常似乎不能正常工作。我设置了以下工作正常的代码——调用WCF restful服务。我决定通过关闭WCF REST服务来测试它,希望它会捕获错误消息或忽略200(OK)消息等。但是由于某些原因,尝试捕获和响应。 据我所知,当200被忽略时,我应该只需要抓住ArgumentOutOfRangeException…思考 // set up passing parameters var p = new HttpQueryString

我有点困惑为什么我的try…catch异常似乎不能正常工作。我设置了以下工作正常的代码——调用WCF restful服务。我决定通过关闭WCF REST服务来测试它,希望它会捕获错误消息或忽略200(OK)消息等。但是由于某些原因,尝试捕获和响应。 据我所知,当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;
        }

在告诉VisualStudio不要中断抛出的异常后,您是否尝试过这样做?从菜单中,调试->异常,然后取消选中抛出


另外,您可能希望从Rest初学者工具包中转储该版本的HttpClient。可以在此处找到最新版本,并警告有突破性的更改。

我应该查看Microsoft.Http吗?我通过nugent下载的版本似乎不适用于我的项目-我看到的是2.45版或其他版本…你能解释一下吗?谢谢,@dawriter我相信它现在在System.Net.Http名称空间中。