如何:以异步方式使用 CloseWorkbook 方法调用

上次修改时间: 2010年3月24日

适用范围: SharePoint Server 2010

当您使用 Excel Web Services 时,如果您已经使用完会话,则通过调用 CloseWorkbook 方法来关闭工作簿是一个很好的做法。这将关闭会话并允许 Excel Services 以可预测的方式释放资源。这样可以潜在地改进服务器的性能和可靠性。

但是,任何 Web 服务调用都需要时间。根据您安装服务器的方式、访问服务器的方式以及服务器所承受的压力,调用所需的时间介于 50 毫秒与 500 毫秒之间。可能还需要更长时间,但仅限服务器的压力非常重的情况。

由于失败的 CloseWorkbook 方法调用不会起任何作用,因此您不需要等待它完成以查看其是否成功。于是,您通常可以通过异步方式进行调用并节省一些操作时间。

备注

如果应用程序对 Excel Services 进行了一些调用,然后退出,则您可能需要以同步方式(而非异步方式)关闭工作簿。在这种情况下,您可以调用 CloseWorkbook 方法(而非 CloseWorkbookAsync 方法)。原因是您在发出异步调用后将立即退出进程,调用获得成功的可能性不高。

要以异步方式关闭工作簿,必须执行两项操作:

  • 请确认您不公开 Excel Web Services 代理类,否则,可能发生 Excel Services 异常。

  • 调用 CloseWorkbookAsync 方法,而不调用 CloseWorkbook 方法。CloseWorkbookAsync 方法的签名为:

    public void CloseWorkbookAsync(string sessionId)
    
    Public Sub CloseWorkbookAsync(ByVal sessionId As String)
    End Sub
    

您不必实现调用 CloseWorkbookAsync 方法时所调用的事件。

可以在项目"Web 引用"目录的"Reference.cs"文件重找到此签名。

备注

您可以在使用 Microsoft Visual Studio 2005 添加 Web 引用时所生成的代理类中找到 CloseWorkbookAsync 方法。如果您使用 Visual Studio 2003,则调用 BeginCloseWorkbook 方法,转而以异步方式关闭工作簿。

调用 CloseWorkbookAsync 方法或 BeginCloseWorkbook 方法意味着关闭工作簿的调用将以异步方式执行,不值得应用程序花费大量的时间。

示例

下面的示例说明如何使用 Visual Studio 2005 以异步方式关闭工作簿。

using System;
using SampleApplication.ExcelWebService;
using System.Web.Services.Protocols;
namespace SampleApplication
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {            
            // Instantiate the Web service 
            // and create a status array object.
            ExcelService es = new ExcelService();
            Status[] outStatus;

            string sheetName = "Sheet1";
            // TODO: change the workbook path to 
            // point to workbook in a trusted location
            // that you have access to. 
            string targetWorkbookPath = 
             "http://myserver02/example/Shared%20Documents/Book1.xlsx";

            // Set credentials for requests.
            es.Credentials = 
                System.Net.CredentialCache.DefaultCredentials;
            
            try
            {
                // Call open workbook, and point to the trusted   
                // location of the workbook to open.
                string sessionId = es.OpenWorkbook(targetWorkbookPath, 
                    "en-US", "en-US", out outStatus);
                // Call the GetCell method 
                // to retrieve a value from a cell.
                // The cell is in the first row and ninth column.
                object[] rangeResult2 = xlservice.GetCell(sessionId, 
                    sheetName, 0, 8, false, out outStatus);
 
                // Close the workbook asynchronously. 
                // This also closes session.
                es.CloseWorkbookAsync(sessionId);
            }
            catch (SoapException e)
            {
                Console.WriteLine("SOAP Exception Message: {0}", 
                   e.Message);
                Console.WriteLine("SOAP Exception Error Code: {0}", 
                   e.SubCode.Code.Name);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception Message: {0}", e.Message);
            }
            // Console.ReadLine();
        }
    }
}
 
Imports System
Imports SampleApplication.ExcelWebService
Imports System.Web.Services.Protocols
Namespace SampleApplication
    Friend Class Class1
        <STAThread> _
        Shared Sub Main(ByVal args() As String)
            ' Instantiate the Web service 
            ' and create a status array object.
            Dim es As New ExcelService()
            Dim outStatus() As Status

            Dim sheetName As String = "Sheet1"
            ' TODO: change the workbook path to 
            ' point to workbook in a trusted location
            ' that you have access to. 
            Dim targetWorkbookPath As String = "http://myserver02/example/Shared%20Documents/Book1.xlsx"

            ' Set credentials for requests.
            es.Credentials = System.Net.CredentialCache.DefaultCredentials

            Try
                ' Call open workbook, and point to the trusted   
                ' location of the workbook to open.
                Dim sessionId As String = es.OpenWorkbook(targetWorkbookPath, "en-US", "en-US", outStatus)
                ' Call the GetCell method 
                ' to retrieve a value from a cell.
                ' The cell is in the first row and ninth column.
                Dim rangeResult2() As Object = xlservice.GetCell(sessionId, sheetName, 0, 8, False, outStatus)

                ' Close the workbook asynchronously. 
                ' This also closes session.
                es.CloseWorkbookAsync(sessionId)
            Catch e As SoapException
                Console.WriteLine("SOAP Exception Message: {0}", e.Message)
                Console.WriteLine("SOAP Exception Error Code: {0}", e.SubCode.Code.Name)
            Catch e As Exception
                Console.WriteLine("Exception Message: {0}", e.Message)
            End Try
            ' Console.ReadLine();
        End Sub
    End Class
End Namespace

强大的编程功能

请确保将 Web 引用添加到您具有访问权限的 Excel Web Services 网站。将 using SampleApplication.ExcelWebService; 语句更改为指向您所引用的 Web 服务网站。

此外,对工作簿路径、工作表名称等进行相应的更改。

请参阅

任务

演练:使用 Excel Web Services 开发自定义应用程序

如何:捕获异常

如何:信任一个位置

如何:从 Excel 客户端保存到服务器

如何:使用 SubCode 属性捕获错误代码

概念

访问 SOAP API

Excel Services 警报

Excel Services 已知问题和提示

环回 SOAP 调用和直接链接