1

我有一个 Excel 插件,当 Excel 启动时,它将访问一个 Web 服务 (GET),它是一个简单的 Web 服务请求,应该立即完成说:类似于https://mywebservice.com&application=myapp&user=currentuser,结果是一个短 (<200 字节)JSON表达式。
如果我在浏览器中运行请求,它确实像预期的那样快。

在我的插件中,我记录了网络请求从开始到结束的时间,通常(大约 40-50% 的时间)需要 3-5 秒,其他时候,它真的像从浏览器运行一样快。

当速度慢时,Excel 没有响应,只需在状态栏中显示“Registering MyaddIn.xll...”即可。

我很困惑,不知道如何调试/解决问题。

谢谢

这是我用来调用 Web 服务的 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;
    }
4

0 回答 0