I noticed an invalid implementation of a reader / writer lock, that I wanted to bring to your attention, due to potential scalability issues.
In the included method below, an exclusive lock on the instance variable, _readerLockerDefs is obtained, preventing any other threads from reading the cache at the same time. The _writerLockerDefs is actually redundant here, since it is also an instance variable.
Either a ReaderWriterLock or a custom implementation of the Reader/Writer pattern is needed.
Cheers,
Stuart
private WorkflowFoundationPageFlowDefinition GetOrCreateDefinition(Type pageFlowType, WorkflowInstance instance)
{
WorkflowFoundationPageFlowDefinition cachedDefinition;
lock (_readerLockerDefs)
{
if (!_workflowDefinitionsCache.TryGetValue(pageFlowType, out cachedDefinition))
{
lock (_writerLockerDefs)
{
if (!_workflowDefinitionsCache.TryGetValue(pageFlowType, out cachedDefinition))
{
cachedDefinition = new WorkflowFoundationPageFlowDefinition((Activities.PageFlow)instance.GetWorkflowDefinition());
_workflowDefinitionsCache.Add(pageFlowType, cachedDefinition);
}
}
}
}
return cachedDefinition;
}