共用方式為


工作 2:啟用長期服務的持續性

在這個工作中,您會使用一些屬性來裝飾您在工作 1 中建立的服務,讓您在每次成功作業叫用後保留您服務的狀態。

使用長期服務屬性裝飾服務

  1. 開啟 Service1.cs (如果您已建立 Visual Basic 方案,則為 Service1.vb)。

  2. 在 [方案總管] 窗格中,在 SimpleDurableService 專案節點下,以滑鼠右鍵按一下 [參考] 子資料夾,並選取 [新增參考]。

  3. 在 [新增參考] 對話方塊的 [.NET] 索引標籤中,選取 [System.WorkflowServices],並按一下 [確定]。

  4. 在您的 C# 原始程式檔中新增下列 using 陳述式:

如果您已建立 Visual Basic 方案,請以滑鼠右鍵按一下 [SimpleDurableService] 專案節點,然後選取 [屬性]。選取 [參考] 索引標籤,然後核取 [匯入的命名空間] 底下的 [System.ServiceModel.Description]。

  1. 若要維持您的服務,您必須使用 DurableServiceAttributeSerializableAttribute 屬性裝飾 Service1 類別,如下列範例所示。

  2. DurableServiceAttribute 屬性指定您 WCF 服務的狀態資訊可保留到持續性存放區,例如資料庫或檔案。此外,服務型別必須能夠序列化,讓它可傳輸到持續性存放區。

  3. 使用 DurableOperationAttribute 屬性裝飾服務作業,如下列範例所示。這個屬性指定服務執行個體狀態將於完成作業後儲存。

CanCreateInstanceCompletesInstance 屬性,會指定長期服務執行個體要在裝飾作業成功完成後再建立或完成。在本教學課程中,有幾項作業以第一項和最後一項作業控制服務執行個體的行為。

DurableOperationAttribute 屬性設為預設值以進行裝飾的作業,只能對持續性存放區中的現存服務執行個體項目持續提供狀態資訊。然而,如果您建立了 System.ServiceModel.SessionMode.NotAllowed 設定為 true 的服務合約,每項 DurableOperationAttribute 屬性 (Attribute) 的 CanCreateInstance 屬性 (Property) 都必須設為 true

現在您的服務已經使用適當的屬性裝飾,您必須建立組態檔,並參考您想要使用的持續性存放區。

設定您的長期服務

  1. 開啟 App.config。

  2. 修改端點組態設定以參考內容繫結,例如 WSHttpContextBinding

    <endpoint address ="" binding="wsHttpContextBinding" contract="SimpleDurableService.IService1" />
    

    必須使用內容繫結,因為用戶端會使用 DurableOperationContext 來識別特定的服務執行個體。在叫用要求/回應作業,並從服務接收回應後,用戶端將在每個後續作業叫用上使用 InstanceId 值,讓用戶端將自己與正確的服務執行個體產生關聯。如果未使用這個追蹤機制,則當服務執行個體關閉或與用戶端中斷連線時,用戶端將無法重新建立與特定服務執行個體的連線。

  3. 將下列子節點新增至您的行為節點。

    <behaviors>
          <serviceBehaviors>
            <behavior name="SimpleDurableService.Service1Behavior">
              <!-- To avoid disclosing metadata information, 
              set the following value to false and remove the preceding metadata endpoint before deployment. -->
              <serviceMetadata httpGetEnabled="True"/>
              <!-- To receive exception details in faults for debugging purposes, 
              set the following value to true. Set the value to false before deployment 
              to avoid disclosing exception information. -->
              <serviceDebug includeExceptionDetailInFaults="False" />
              <persistenceProvider type="System.ServiceModel.Persistence.SqlPersistenceProviderFactory, System.WorkflowServices, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                                   connectionStringName="DurableServiceStore" 
                                   persistenceOperationTimeout = "00:00:10"
                                   lockTimeout="00:01:00"
                                   serializeAsText="true"/>
    
            </behavior>
          </serviceBehaviors>
        </behaviors>
    

    PersistenceProviderElement 定義您服務將使用之 PersistenceProvider 的型別。在本教學課程中,SqlPersistenceProviderFactory 會用來建立與 SQL 資料庫連線之 PersistenceProvider 型別的新執行個體。只要叫用新增作業並成功完成時,將建立新服務執行個體,且其狀態資訊將儲存在 SQL 資料庫中。

  4. 新增您服務執行個體將用於持續性之 SQL 資料庫的連線字串。

      </system.serviceModel>
      <connectionStrings>
        <add name="DurableServiceStore" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=NetFx35Samples_DurableServiceStore;Integrated Security=SSPI"/>
      </connectionStrings>
    </configuration>
    
Bb675272.note(zh-tw,VS.90).gif注意:
本教學課程使用的資料庫與Durable Workflow Services Sample所使用的相同,因此連線字串會相同。若要允許存取長期存放區,請從 One-Time Setup Procedure for the Windows Communication Foundation Samples執行 Createstores.cmd 指令碼。

在下一個工作中,您將建立可存取您長期服務的用戶端。

請參閱

工作

工作 1:定義和實作長期服務合約
工作 3:建立長期服務用戶端

其他資源

教學課程:建立長期服務

Copyright © 2007 by Microsoft Corporation.All rights reserved.