【问题标题】:Using 3rd party API in a ASP.NET MVC 3 website在 ASP.NET MVC 3 网站中使用 3rd 方 API
【发布时间】:2013-09-20 10:37:48
【问题描述】:

在我们的网站中,我们通过 API 从ZendeskFlickrStopForumSpam 中提取内容。当这些服务器关闭时,我们如何防止我们的网站被阻塞?

谢谢。

【问题讨论】:

    标签: asp.net asp.net-mvc web-services


    【解决方案1】:

    在您的 API 调用中实施合理的超时政策。您需要确保在加载时间比平时更长的内容和不可用的内容之间取得平衡。

    【讨论】:

      【解决方案2】:

      如果您使用的是 MVC4/.NET 4.5+,您可以使用 async and await 调用远程服务,这将允许您的应用程序在等待网络 I/O 完成的同时处理其他操作。

      编辑:您仍然需要提供逻辑来管理错误/超时,但在等待触发超时时您不会锁定尽可能多的资源。

      如果您四处搜索,这里有很多教程涵盖了使用 async/await 来执行网络 IO 任务。

      Edit2: Valverij 指出(从问题的标题)你显然不能使用 MVC4/.NET 4.5+,所以你需要使用更详细的 Asynchronous Controller 方法来达到同样的效果。

      【讨论】:

      • 您提供的所有链接都说明了如何运行异步方法,1000 毫秒后退出的示例怎么样?
      【解决方案3】:

      这是迄今为止找到的最佳解决方案:

      public static bool IsReady(string uri)
      {
         // Create a new 'HttpWebRequest' Object to the mentioned URL.
         HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create(uri);
      
         // Set the 'Timeout' property of the HttpWebRequest to 1000 milliseconds.
         myHttpWebRequest.Timeout = 1000;
      
         HttpWebResponse myHttpWebResponse;
      
         try
         {
            // A HttpWebResponse object is created and is GetResponse Property of the HttpWebRequest associated with it 
            myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
         }
         catch (Exception ex)
         {
            Debug.WriteLine("Error: " + ex.Message);
            return false;
         }
      
         return myHttpWebResponse.StatusCode == HttpStatusCode.OK;
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-17
        • 1970-01-01
        相关资源
        最近更新 更多