Partilhar via


CCR tips and tricks - part 21

When working with resource ports as in part 19 and 20 there is another thing you may need to do if you rely on causalities. Before reposting the resource (or enqueue a new timer) you should do this:

  1: Dispatcher.ClearCausalities();

That call removes all causalities from the current thread. This is important since otherwise you may leak causalities through your resource. The reason is that when the resource is reposted any causalities will be stored with the resource in the port. When the joined receiver handler executes it's going to merge the causalities from the resource with the causalities from the argument port. If all items on the argument part all use the same causality there is no problem since the merge will see that both ports have the same causality and merge them into one. However if each argument have its own causality (common case when using CCR in web services for example) it's going to be a problem; The first time the joined receiver executes one causality from the argument port is merged with nothing from the resource port. When the resource is reposted the causality travels with it. The second time this resource is join received a new causality is joined with the old causality and now we have two causalities being posted with the resource port and so on. So without clearing the causalities you've created a causality leak that eventually will generate an unhandled exception and your code stops working.