Share via


CloseWorkbook メソッド呼び出しを非同期的に呼び出す

最終更新日: 2010年3月24日

適用対象: SharePoint Server 2010

Excel Web Services を使用しているときに、セッションの使用が終了した場合は、CloseWorkbook メソッドを呼び出してブックを閉じることをお勧めします。これにより、セッションが閉じて、Excel Services は予測可能な方法でリソースを解放することができます。この結果、サーバーのパフォーマンスと信頼性が向上する可能性があります。

ただし、Web サービスの呼び出しには時間がかかります。サーバーをインストールした方法、サーバーにアクセスする方法、およびサーバーが受ける負荷の量に応じて、呼び出しには 50 ~ 500 ミリ秒の時間がかかる場合があります。サーバーの負荷が大きい場合は、さらに時間かかる可能性があります。

失敗した CloseWorkbook メソッドの呼び出しは動作しないので、終了するまで待機して成功しているかどうかを確認する必要はありません。このため、通常は非同期的に呼び出しを行い、処理時間を節約することができます。

注意

アプリケーションがなんらかの Excel Services の呼び出しを行ってから終了する場合は、非同期的にではなく、同期的にブックを閉じる必要があります。この場合は、CloseWorkbookAsync メソッドではなく CloseWorkbook メソッドを呼び出します。これは、非同期呼び出しの実行後、直ちに処理を終了する場合に、呼び出しが完全に終了できない可能性があるためです。

ブックを非同期的に閉じるには、次の 2 つのことを行う必要があります。

  • Excel Web Services プロキシ クラスを破棄しないことを確認します。破棄する場合は、非 Excel Services 例外が発生します。

  • CloseWorkbook メソッドではなく、CloseWorkbookAsync メソッドを呼び出します。CloseWorkbookAsync メソッドの署名は次のとおりです。

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

CloseWorkbookAsync メソッドを呼び出すときに、呼び出されるイベントを実装する必要はありません。

署名は "Web References" ディレクトリの "Reference.cs" ファイルで確認できます。

注意

CloseWorkbookAsync メソッドは、Microsoft Visual Studio 2005 を使用して Web 参照を追加するときに生成されるプロキシ クラス内にあります。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

堅牢なプログラミング

アクセス可能な Excel Web Services サイトに Web 参照が追加されていることを確認します。参照する Web サービス サイトを指すように using SampleApplication.ExcelWebService; ステートメントを変更します。

また、ブックのパス、シート名などを必要に応じて変更します。

関連項目

タスク

[ウォークスルー] Excel Web Services を使用してカスタム アプリケーションを開発する

[方法] 例外を検出する

[方法] 場所を信頼する

[方法] Excel クライアントからサーバーに保存する

[方法] SubCode プロパティを使用してエラー コードをキャプチャする

概念

SOAP API にアクセスする

Excel Services の警告

Excel Services に関する既知の問題とヒント

ループバック SOAP 呼び出しと直接リンク