提高多个REST请求的性能

时间:2017-08-24 20:31:37

标签: c# excel vsto webrequest servicenow

我是REST和VSTO的新手。我创建了第一个执行Web请求的excel加载项,并将信息放入excel中。用户通过将它们放在excel上的列中来指定他们想要的票证。 add-inthen执行webrequest以根据该列中的内容获取信息。然后它将该信息写入excel。

问题是我联系的API只允许您一次发送一张票。问题是它很慢。它会冻结约3秒钟,然后每次进入约1.5秒。我逐步完成了我的项目,大部分时间都在进行webrequest。任何人都可以建议更好的替代我的代码吗?

我在哪里完成所有工作。

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;
        }

1 个答案:

答案 0 :(得分:0)

您可以尝试使用Tasks命名空间生成多个同时调用,但最终可能会使您重载Web服务器。值得进行一项实验,看看它的通信滞后是否导致客户端响应缓慢或者您的Web服务器是否运行缓慢。要做到这一点,您可以安装免费工具Fiddler来观察从Excel客户端到Web服务器的呼叫/响应模式 - 它会让您知道花费的时间。如果其网络滞后,则生成多个同时呼叫将有助于缩短总时间。