веб-запрос выполняется во время запуска Excel в Excel AddIn - PullRequest
1 голос
/ 06 декабря 2011

У меня есть Excel AddIn, когда Excel запускается, он получает доступ к веб-службе (GET), это простой запрос веб-службы и должен сразу же закончить, говорит: что-то вроде https://mywebservice.com&application=myapp&user=currentuser, результат короткий (<200 байтов ) <code>JSON выражение.
Если я запускаю запрос в браузере, он работает очень быстро, как и ожидалось.

В моем AddIn я записал время от начала до конца веб-запроса, часто (около 40-50% времени) это занимает 3-5 секунд, в других случаях это очень быстро, как запуск из браузера.

Когда это медленно, Excel не отвечает, просто покажите «Регистрация MyaddIn.xll ...» в строке состояния.

Я так растерялся и не уверен, как отладить / исправить проблему.

спасибо

Вот C #, который я использую для вызова веб-службы

        private static int DownloadInfoFromServer(string entUrl, string localFilename)
    {
        // Function will return the number of bytes processed
        // to the caller. Initialize to 0 here.
        int bytesProcessed = 0;

        // Assign values to these objects here so that they can
        // be referenced in the finally block
        Stream remoteStream = null;
        Stream localStream = null;
        HttpWebResponse response = null;
        HttpWebRequest request;
        // Use a try/catch/finally block as both the WebRequest and Stream
        // classes throw exceptions upon error
        try
        {
            //clear out local file every time no matter request fails or not
            localStream = File.Create(localFilename);

            request = ServiceBase.GetHttpWebRequestWithProxyForEnt(entUrl);
            response = (HttpWebResponse)request.GetResponse();

            // Once the WebResponse object has been retrieved,
            // get the stream object associated with the response's data
            remoteStream = response.GetResponseStream();

            if (remoteStream != null)
            {
                // Allocate a 1k buffer
                var buffer = new byte[1024];
                int bytesRead;

                // Simple do/while loop to read from stream until
                // no bytes are returned
                do
                {
                    // Read data (up to 1k) from the stream
                    bytesRead = remoteStream.Read(buffer, 0, buffer.Length);

                    // Write the data to the local file
                    localStream.Write(buffer, 0, bytesRead);

                    // Increment total bytes processed
                    bytesProcessed += bytesRead;
                } while (bytesRead > 0);
            }
        }
        catch (Exception e)
        {
            Helper.LogError(e);
        }
        finally
        {
            if (response != null) response.Close();
            if (remoteStream != null) remoteStream.Close();
            if (localStream != null) localStream.Close();
        }

        // Return total bytes processed to caller.
        return bytesProcessed;
    }
...