2017-08-24 92 views
0

我是REST和VSTO的新手。我做了我的第一個excel插件,它執行web請求並將信息放入excel。用戶通過將它們放入Excel中的列來指定他們想要的票。 add-inthen會根據該列中的內容執行webrequest以獲取信息。然後它將該信息寫入excel。提高多個REST請求的性能

問題是我聯繫的API只允許您一次發送一張票。問題在於速度緩慢。它凍結約3秒鐘,然後每個條目大約需要1.5秒。我逐步完成了我的項目,並且大部分時間都在製作網絡請求。任何人都可以提出更好的替代我的代碼?

我在哪裏做所有的工作。

public partial class RibbonMain 
{ 
    private void RibbonMain_Load(object sender, RibbonUIEventArgs e) 
    { 

    } 

    //Gather Incident list 
    //collect incident information 
    //write the results to excel 
    private void btnGetServiceNowInfo_Click(object sender, RibbonControlEventArgs e) 
    { 
     //Get active worksheet 
     ProgressForm progressForm = new ProgressForm(); 
     progressForm.Show(); 
     Excel.Worksheet activeWorksheet = Globals.ThisAddIn.GetActiveWorksheet(); 
     int intRow = 2; 
     rClient rClient = new rClient(); 
     rClient.httpUsername = Settings.Default["Username"].ToString(); 
     rClient.httpPassword = Settings.Default["Password"].ToString(); 
     string strReponse = string.Empty; 

     //iterate through incident row 
     //adds incident numbers to list 
     while (true) 
     { 
      Range intRange = activeWorksheet.get_Range("D" + intRow); 
      if (intRange.Value == null) 
      { 
       break; 
      } 
      Range numRange = activeWorksheet.get_Range("A" + intRow); 
      Range descRange = activeWorksheet.get_Range("B" + intRow); 
      Range sys_idRange = activeWorksheet.get_Range("C" + intRow); 
      rClient.endPoint = "https://dev32395.service-now.com/api/now/table/incident?sysparm_query=number%3D" + intRange.Value + "&sysparm_fields=number%2Csys_id%2Cdescription&sysparm_limit=1"; 
      strReponse = rClient.makeReqest(); 
      var deserializedResult = JsonConvert.DeserializeObject<JsonResult>(strReponse); 
      var jsonResult = deserializedResult.result.First(); 
      numRange.Value = jsonResult.number; 
      descRange.Value = jsonResult.description; 
      sys_idRange.Value = jsonResult.sys_id; 
      intRow++; 
     } 
     lstIncidentNum.Clear(); 
     progressForm.Hide(); 
    } 

    //call settings form 
    private void btnSettings_Click(object sender, RibbonControlEventArgs e) 
    { 
     SettingForm settingForm = new SettingForm(); 
     settingForm.Show(); 
    } 
} 

此行using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())是它花費2000 MS對於所述第一請求和大約300 MS其他請求。

public enum httpVerb 
    { 
     GET, 
     POST, 
     PUT, 
     DELETE 
    } 

    class rClient 
    { 
     public string endPoint { get; set; } 
     public httpVerb httpMethod { get; set; } 
     public string httpUsername { get; set; } 
     public string httpPassword { get; set; } 

     public rClient() 
     { 
      endPoint = string.Empty; 
      httpMethod = httpVerb.GET; 
      httpUsername = string.Empty; 
      httpPassword = string.Empty; 
     } 

     public string makeReqest() 
     { 
      string strResponseValue = string.Empty; 

      HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(endPoint); 

      webRequest.Method = httpMethod.ToString(); 

      string Credentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(httpUsername + ":" + httpPassword)); 

      webRequest.Headers.Add("Authorization", "Basic " + Credentials); 

      using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) 
      { 
       if(webResponse.StatusCode != HttpStatusCode.OK) 
       { 
        throw new ApplicationException("Error: " + webResponse.StatusCode.ToString()); 
       } 

       using(Stream responseStream = webResponse.GetResponseStream()) 
       { 
        if (responseStream != null) 
        { 
         using (StreamReader reader = new StreamReader(responseStream)) 
         { 
          strResponseValue = reader.ReadToEnd(); 
         } 
        } 
       } 
      } 
      return strResponseValue; 
     } 

回答

0

您可以嘗試使用任務命名空間來生成多個同時調用,但所有可能最終都會超載您的Web服務器。它值得進行實驗,看看它的通信滯後是否會導致客戶端響應緩慢,或者您的web服務器是否緩慢運行。要做到這一點,你可以安裝免費的工具Fiddler來觀看從Excel客戶端到Web服務器的調用/響應模式 - 它會給你一個想法,在哪裏花費時間。如果網絡滯後,那麼產生多個同時呼叫將有助於縮短總時間。

+0

我會研究它,servicenow是基於雲的,所以不知道我是否會超載它們。當然不是。 – ffalor