更改发送活动的缓存共享级别

SendMessageChannelCache 扩展使你能够自定义缓存共享级别、通道工厂缓存的设置,以及用于使用 Send 消息传送活动将消息发送到服务终结点的工作流的通道缓存的设置。 这些工作流通常是客户端工作流,但也可以是托管在一个 WorkflowServiceHost工作流中的工作流服务。 通道工厂缓存包含缓存 ChannelFactory<TChannel> 的对象。 通道缓存包含已缓存的通道。

注释

工作流可以使用 Send 消息活动发送消息或参数。 工作流运行时将通道工厂添加到缓存中,当您使用ReceiveReply活动和Send活动时,会创建IRequestChannel类型的通道;而仅使用Send活动(没有ReceiveReply活动)时,会创建IOutputChannel类型的通道。

缓存共享级别

默认情况下,在 WorkflowServiceHost 所承载的工作流中,由 Send 消息传递活动使用的缓存可在 WorkflowServiceHost 中的所有工作流实例之间共享(宿主级缓存)。 对于不是由 a WorkflowServiceHost托管的客户端工作流,缓存仅适用于工作流实例(实例级缓存)。 缓存仅适用于 Send 不使用配置中定义的终结点的活动,除非启用了不安全的缓存。

以下是可用于 Send 工作流中的活动的不同缓存共享级别及其建议用途:

  • 主机级别:在主机共享级别中,缓存仅适用于工作流服务主机中托管的工作流实例。 还可以在进程范围内的缓存中,共享工作流服务主机之间的缓存。

  • 实例级别:在实例共享级别中,缓存在其整个生存期内可供特定工作流实例使用,但缓存不适用于其他工作流实例。

  • 无缓存:如果工作流使用配置中定义的终结点,则默认关闭缓存。 在这种情况下,还建议关闭缓存,因为打开缓存可能不安全。 例如,如果每次发送都需要使用不同的身份(不同的凭据或进行身份模拟)。

更改客户端工作流的缓存共享级别

若要在客户端工作流中设置缓存共享,请将类的 SendMessageChannelCache 实例添加为所需工作流实例集的扩展。 这会导致在所有工作流实例之间共享缓存。 以下代码示例演示如何执行这些步骤。

首先,声明类型的 SendMessageChannelCache实例。

// Create an instance of SendMessageChannelCache with default cache settings.  
static SendMessageChannelCache sharedChannelCacheExtension =  
    new SendMessageChannelCache();  

接下来,将缓存扩展添加到每个客户端工作流实例。

WorkflowApplication clientInstance1 = new WorkflowApplication(new clientWorkflow1());  
WorkflowApplication clientInstance2 = new WorkflowApplication(new clientWorkflow2());  
  
// Share the cache extension object
  
clientInstance1.Extensions.Add(sharedChannelCacheExtension);  
clientInstance2.Extensions.Add(sharedChannelCacheExtension);  

更改托管工作流服务的缓存共享级别

若要在托管工作流服务中设置缓存共享,请将类的 SendMessageChannelCache 实例添加为所有工作流服务主机的扩展。 这会导致在所有工作流服务主机之间共享缓存。 下面的代码示例演示如何执行这些步骤。

首先,在类级别声明类型 SendMessageChannelCache 实例。

// Create static instance of SendMessageChannelCache with default cache settings.  
static SendMessageChannelCache sharedChannelCacheExtension = new  
    SendMessageChannelCache();  

接下来,将静态缓存扩展添加到每个工作流服务主机。

WorkflowServiceHost host1 = new WorkflowServiceHost(new serviceWorkflow1(), new Uri(baseAddress1));  
WorkflowServiceHost host2 = new WorkflowServiceHost(new serviceWorkflow2(), new Uri(baseAddress2));  
  
// Share the static cache to get an AppDomain level cache.  
host1.WorkflowExtensions.Add(sharedChannelCacheExtension);  
host2.WorkflowExtensions.Add(sharedChannelCacheExtension);  

若要将托管工作流服务中的缓存共享设置为实例级别,请将一个Func<SendMessageChannelCache>委托作为扩展添加到工作流服务主机,并将此委托分配给用于实例化SendMessageChannelCache类新实例的代码。 这会导致每个单个工作流实例使用不同的缓存,而不是工作流服务主机中所有工作流实例共享的单个缓存。 下面的代码示例演示如何使用 lambda 表达式直接定义委托指向的 SendMessageChannelCache 扩展来实现此目的。

serviceHost.WorkflowExtensions.Add(() => new SendMessageChannelCache  
{  
    // Use FactorySettings property to add custom factory cache settings.  
    FactorySettings = new ChannelCacheSettings
    { MaxItemsInCache = 5, },  
    // Use ChannelSettings property to add custom channel cache settings.  
    ChannelSettings = new ChannelCacheSettings
    { MaxItemsInCache = 10 },  
});  

自定义缓存设置

可以自定义通道工厂缓存和通道缓存的缓存设置。 缓存设置在类中 ChannelCacheSettings 定义。 该 SendMessageChannelCache 类在其无参数构造函数中定义通道工厂缓存和通道缓存的默认缓存设置。 下表列出了每种缓存类型的缓存设置的默认值。

设置 LeaseTimeout(分钟) IdleTimeout(分钟) 缓存中最大项目数
工厂缓存默认值 TimeSpan.MaxValue(时间跨度的最大值) 2 16
通道缓存默认值 5 2 16

若要自定义工厂缓存和通道缓存设置,请使用参数化构造函数实例化SendMessageChannelCache类,并将具有自定义值的新实例ChannelCacheSettings传递给每个factorySettingschannelSettings参数。SendMessageChannelCache 接下来,将此类的新实例作为扩展添加到工作流服务主机或工作流实例。 下面的代码示例演示如何对工作流实例执行这些步骤。

ChannelCacheSettings factorySettings = new ChannelCacheSettings{  
                        MaxItemsInCache = 5,
                        IdleTimeout = TimeSpan.FromMinutes(5),
                        LeaseTimeout = TimeSpan.FromMinutes(20)};  
  
ChannelCacheSettings channelSettings = new ChannelCacheSettings{  
                        MaxItemsInCache = 5,
                        IdleTimeout = TimeSpan.FromMinutes(2),  
                        LeaseTimeout = TimeSpan.FromMinutes(10) };  
  
SendMessageChannelCache customChannelCacheExtension =
    new SendMessageChannelCache(factorySettings, channelSettings);  
  
clientInstance.Extensions.Add(customChannelCacheExtension);  

当工作流服务的配置中定义了终结点时,如果要启用缓存,请使用参数化构造函数 SendMessageChannelCache 实例化 SendMessageChannelCache 类,并将 allowUnsafeCaching 参数设置为 true。 接下来,将此类的新实例作为扩展添加到工作流服务主机或工作流实例。 下面的代码示例演示如何为工作流实例启用缓存。

SendMessageChannelCache customChannelCacheExtension =
    new SendMessageChannelCache{ AllowUnsafeCaching = true };  
  
clientInstance.Extensions.Add(customChannelCacheExtension);  

若要完全禁用通道工厂和通道的缓存,请禁用通道工厂缓存。 这样做也会关闭通道缓存,因为通道由相应的通道工厂拥有。 若要禁用通道工厂缓存,请将 factorySettings 参数传递给 SendMessageChannelCache 构造函数,该构造函数初始化为一个值为 0 的 MaxItemsInCacheChannelCacheSettings 实例。 下面的代码示例演示了这一点。

// Disable the factory cache. This results in the channel cache to be turned off as well.  
ChannelCacheSettings factorySettings = new ChannelCacheSettings  
    { MaxItemsInCache = 0 };  
  
ChannelCacheSettings channelSettings = new ChannelCacheSettings();  
  
SendMessageChannelCache customChannelCacheExtension =
    new SendMessageChannelCache(factorySettings, channelSettings);
  
clientInstance.Extensions.Add(customChannelCacheExtension);  

可以选择仅使用通道工厂缓存,并通过向将值为0的MaxItemsInCache实例初始化为ChannelCacheSettings的构造函数传递channelSettings参数来禁用通道缓存SendMessageChannelCache。 下面的代码示例演示了这一点。

ChannelCacheSettings factorySettings = new ChannelCacheSettings();  
// Disable only the channel cache.  
ChannelCacheSettings channelSettings = new ChannelCacheSettings  
    { MaxItemsInCache = 0};  
  
SendMessageChannelCache customChannelCacheExtension =
    new SendMessageChannelCache(factorySettings, channelSettings);
  
clientInstance.Extensions.Add(customChannelCacheExtension);  

在托管工作流服务中,可以在应用程序配置文件中指定工厂缓存和通道缓存设置。 为此,请添加一个包含工厂和通道缓存设置的服务行为,并将其添加到您的服务中。 以下示例显示了包含 MyChannelCacheBehavior 具有自定义工厂缓存和通道缓存设置的服务行为的配置文件的内容。 此服务行为通过 behaviorConfiguration 属性添加到服务。

<configuration>
  <system.serviceModel>  
    <!-- List of other config sections here -->
    <behaviors>  
      <serviceBehaviors>  
        <behavior name="MyChannelCacheBehavior">  
          <sendMessageChannelCache allowUnsafeCaching ="false" >  
            <!-- Control only the host level settings -->
            <factorySettings maxItemsInCache = "8" idleTimeout = "00:05:00" leaseTimeout="10:00:00" />  
            <channelSettings maxItemsInCache = "32" idleTimeout = "00:05:00" leaseTimeout="00:06:00" />  
          </sendMessageChannelCache>  
        </behavior>  
      </serviceBehaviors>  
    </behaviors>  
    <services>  
      <service name="MyService" behaviorConfiguration="MyChannelCacheBehavior" />  
    </services>  
  </system.serviceModel>  
</configuration>