【问题标题】:How to use 'using' with 'out' parameter如何使用带有 'out' 参数的 'using'
【发布时间】:2012-07-05 07:20:31
【问题描述】:

我有这样的方法

private bool VerbMethod(string httpVerb, string methodName, string url, string command, string guid, out HttpWebResponse response)

我是这样用的

  HttpWebResponse response;
  if (VerbMethod("POST", "TheMethod", "http://theurl.com", "parameter1=a", theGuid, out response))
  {
    using (StreamReader sr = new StreamReader(response.GetResponseStream()))
    {
      string responseString = sr.ReadToEnd();
    }

它返回一个布尔值来指定方法是否运行良好,并在一个out参数中设置响应以获取数据。

我有时会超时,然后后续请求也会超时。我看到了这个 WebRequest.GetResponse locks up?

它推荐using 关键字。问题是,使用上述方法签名我不知道该怎么做。

  • 我应该在 finally 中手动调用 dispose 吗?
  • 有没有办法仍然使用usingout 参数?
  • 重写方法,使其不暴露HttpWebResponse?

【问题讨论】:

    标签: c# timeout using httpwebresponse out


    【解决方案1】:

    它返回一个布尔值来指定方法是否顺利

    那是你的问题。不要使用布尔成功值:如果出现问题,则抛出异常。 (或者更确切地说,让异常冒泡。)

    只需更改返回响应的方法即可。

    【讨论】:

      【解决方案2】:

      如果你想使用using(没有例外),只需交换布尔值和响应:

      private HttpWebResponse VerbMethod(string httpVerb, string methodName, string url, string command, string guid, out bool canExecute);
      
      
      bool canExecute = false;
      
      using(HttpWebResponse response = VerbMethod("POST", "TheMethod", "http://theurl.com", "parameter1=a", theGuid, out canExecute))
      {
        if (canExecute)
        {
          ..
        }
      }
      

      【讨论】:

        【解决方案3】:

        在函数的开头立即为out参数分配默认值,并继续使用您已经使用的using

        【讨论】:

          【解决方案4】:

          你也可以使用

              HttpWebResponse response;
              if (VerbMethod("POST", "TheMethod", "http://theurl.com", "parameter1=a", theGuid, out response))
              {
                  using (response)
                  {
                      using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()))
                      {
                          string responseString = sr.ReadToEnd();
                      }
                  }
              }
          

          【讨论】:

            【解决方案5】:

            您可以添加另一个using 进行回复:

            HttpWebResponse response; 
            if (VerbMethod("POST", "TheMethod", "http://theurl.com", "parameter1=a", theGuid,
               out response)) 
            { 
              using(response)
              {
                using (StreamReader sr = new StreamReader(response.GetResponseStream())) 
                { 
                  string responseString = sr.ReadToEnd(); 
                } 
              }
            }
            

            【讨论】:

              【解决方案6】:

              可以这样做:

              private bool VerbMethod(string httpVerb, string methodName, string url, 
                string command, string guid, out HttpWebResponse response) {}
              
              HttpWebResponse response = null;
              
              if(VerbMethod(httpVerb, methodName, url, command, guid, out response) {
                using(response)
                {
                  using (StreamReader sr = new StreamReader(response.GetResponseStream())) {
                  }
                }
              }
              

              using 语句不要求其中的表达式是 new 对象或方法返回 - 任何表达式都可以。

              但是 - 通常,在您调用 GetResponseStream() 之前,请求不会触发,所以我看不到您的 bool 返回实际上除了确认对象已创建之外还有其他任何作用 - 并且单元中没有任何意义测试运行时(!)。因此最好的办法是让该方法返回响应并将其放在using 中。我可以从其他答案中看出我并不孤单。

              然而,同样的论点可以用来证明我在上面列出的改变是合理的。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2017-06-11
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-05-11
                • 1970-01-01
                相关资源
                最近更新 更多