使用 SharedConnectionWorkflowCommitWorkBatchService

运行时引擎启动后,如果没有添加其他 WorkflowCommitWorkBatch 服务,则 WorkflowRuntime 类将创建 DefaultWorkflowCommitWorkBatchService 对象。 您可以在工作流中使用此服务以支持数据完整性所需的工作批次。

也可以选择使用 SharedConnectionWorkflowCommitWorkBatchService 服务。 此服务用于在不同对象之间使用共享连接的数据库事务。 若要在 WorkflowRuntime 实例中使用 SharedConnectionWorkflowCommitWorkBatchService 服务而不是 DefaultWorkflowCommitWorkBatchService 服务,请使用 AddService 方法,如下例所示。 SharedConnectionWorkflowCommitWorkBatchService 构造函数的参数是数据库连接字符串。

备注

如果 SharedConnectionWorkflowCommitWorkBatchService 服务所用的 SQL Server 已关闭(例如由于 SQL 群集故障转移或间歇性网络问题所致),则 SharedConnectionWorkflowCommitWorkBatchService 服务将重试提交过程至少 15 至 20 次,然后引发 ServicesExceptionNotHandled 事件。 但是,仅当 SharedConnectionWorkflowCommitWorkBatchService 服务的 EnableRetries 属性设置为 true 时,才会发生此行为。

static void Main(string[] args)
{
    string connectionString = " Initial Catalog=WorkflowDataStore;Data Source=localhost;Integrated Security=SSPI;";

    WorkflowRuntime workflowRuntime = new WorkflowRuntime();

    workflowRuntime.AddService( new SharedConnectionWorkflowCommitWorkBatchService(connectionString));
    workflowRuntime.StartRuntime();
    
    // …

    workflowRuntime.StopRuntime();
}
Shared Sub Main(ByVal args() As String) 
    Dim connectionString As String = " Initial Catalog=WorkflowDataStore;Data Source=localhost;Integrated Security=SSPI;"
    
    Dim workflowRuntime As New WorkflowRuntime()
    
    workflowRuntime.AddService(New SharedConnectionWorkflowCommitWorkBatchService(connectionString))
    workflowRuntime.StartRuntime()
    
    ' ...
    workflowRuntime.StopRuntime()
End Sub 'Main

使用应用程序配置文件

您还可以使用应用程序配置文件来创建 SharedConnectionWorkflowCommitWorkBatchService 对象。 采用这种方式时,需要将连接字符串信息添加到 app.config 文件的 CommonParameters 节。 下面的示例演示一个使用 SharedConnectionWorkflowCommitWorkBatchService 服务的应用程序配置文件。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="WorkflowServiceContainer" type="System.Workflow.Runtime.Configuration.WorkflowRuntimeSection, System.Workflow.Runtime, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </configSections>
    <WorkflowServiceContainer Name="Container Name" UnloadOnIdle="true">
        <CommonParameters>
            <add name="ConnectionString" value="Initial Catalog=WorkFlowStore;Data Source=localhost;Integrated Security=SSPI;" />
        </CommonParameters>
        <Services>
            <add type="System.Workflow.Runtime.Hosting.DefaultWorkflowSchedulerService, System.Workflow.Runtime, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
            <add type="System.Workflow.Runtime.Hosting.SharedConnectionWorkflowTransactionService, System.Workflow.Runtime, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
            <add type="System.Workflow.Runtime.Tracking.SqlTrackingService, System.Workflow.Runtime, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            <add type="System.Workflow.Runtime.Hosting.SqlWorkflowPersistenceService, System.Workflow.Runtime, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        </Services>
    </WorkflowServiceContainer>
    <system.diagnostics>
    </system.diagnostics>
</configuration>

请参见

参考

DefaultWorkflowCommitWorkBatchService
SharedConnectionWorkflowCommitWorkBatchService

概念

服务概述
Windows 工作流 CommitWorkBatch 服务

其他资源

Using WorkflowCommitWorkBatch Service Sample

Footer image

版权所有 (C) 2007 Microsoft Corporation。保留所有权利。