Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / TransactionBridge / Microsoft / Transactions / Wsat / Protocol / LookupTables.cs / 1 / LookupTables.cs
//------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
// This file contains all transaction and enlistment lookup tables for WS-AT
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using Microsoft.Transactions.Bridge;
using Microsoft.Transactions.Wsat.Messaging;
namespace Microsoft.Transactions.Wsat.Protocol
{
class LookupTables
{
ProtocolState state;
ReaderWriterLock transactionLock = new ReaderWriterLock();
ReaderWriterLock enlistmentLock = new ReaderWriterLock();
Dictionary enlistments = new Dictionary();
Dictionary transactions = new Dictionary();
public LookupTables(ProtocolState state)
{
this.state = state;
}
//
// Enlistments
//
public TransactionEnlistment FindEnlistment(Guid enlistmentId)
{
return Find(this.enlistments,
this.enlistmentLock,
enlistmentId);
}
public void AddEnlistment(TransactionEnlistment enlistment)
{
Add(this.enlistments,
this.enlistmentLock,
enlistment.EnlistmentId,
enlistment);
}
public void RemoveEnlistment(TransactionEnlistment enlistment)
{
Remove(this.enlistments,
this.enlistmentLock,
enlistment.EnlistmentId,
enlistment);
}
//
// Transaction contexts
//
public TransactionContextManager FindTransactionContextManager(string contextId)
{
return Find(this.transactions,
this.transactionLock,
contextId);
}
public void AddTransactionContextManager(TransactionContextManager contextManager)
{
Add(this.transactions,
this.transactionLock,
contextManager.Identifier,
contextManager);
}
public TransactionContextManager FindOrAddTransactionContextManager(TransactionContextManager contextManager,
out bool found)
{
return FindOrAdd(this.transactions,
this.transactionLock,
contextManager.Identifier,
contextManager,
out found);
}
public void RemoveTransactionContextManager(TransactionContextManager contextManager)
{
Remove(this.transactions,
this.transactionLock,
contextManager.Identifier,
contextManager);
}
//
// Internal
//
void Add(Dictionary dictionary, ReaderWriterLock rwLock, T key, S value)
{
bool lockHeld = false;
try
{
try { }
finally
{
rwLock.AcquireWriterLock(Timeout.Infinite);
lockHeld = true;
}
dictionary.Add(key, value);
}
finally
{
if (lockHeld)
{
rwLock.ReleaseWriterLock();
}
}
if (DebugTrace.Verbose)
{
int count = dictionary.Count;
DebugTrace.Trace(TraceLevel.Verbose,
"Added {0} {1} to lookup table. Table contains {2} object{3}",
value.GetType().Name, value,
count, count == 1 ? string.Empty : "s");
}
}
void Remove(Dictionary dictionary, ReaderWriterLock rwLock, T key, S value)
{
bool lockHeld = false;
try
{
try { }
finally
{
rwLock.AcquireWriterLock(Timeout.Infinite);
lockHeld = true;
}
#if DEBUG
S existing;
if (dictionary.TryGetValue(key, out existing) && !ReferenceEquals(value, existing))
{
// If the value exists but isn't the same value as the item passed in,
// we have a bug. Reason: an assumption in the code is violated.
DiagnosticUtility.DebugAssert("The lookup table contains a different object");
}
#endif
bool removed = dictionary.Remove(key);
if (!(removed))
{
// If the value doesn't exist in the dictionary,
// we have a bug. Reason: an assumption in the code is violated.
DiagnosticUtility.FailFast("The lookup table does not contain the object");
}
}
finally
{
if (lockHeld)
{
rwLock.ReleaseWriterLock();
}
}
if (DebugTrace.Verbose)
{
int count = dictionary.Count;
DebugTrace.Trace(TraceLevel.Verbose,
"Removed {0} {1} from lookup table. Table contains {2} object{3}",
value.GetType().Name, value,
count, count == 1 ? string.Empty : "s");
}
}
S Find(Dictionary dictionary, ReaderWriterLock rwLock, T key) where S : class
{
S value;
bool lockHeld = false;
try
{
try { }
finally
{
rwLock.AcquireReaderLock(Timeout.Infinite);
lockHeld = true;
}
if (!dictionary.TryGetValue(key, out value))
{
value = null;
}
}
finally
{
if (lockHeld)
{
rwLock.ReleaseReaderLock();
}
}
return value;
}
S FindOrAdd(Dictionary dictionary, ReaderWriterLock rwLock, T key, S value, out bool found)
{
S inserted;
bool lockHeld = false;
try
{
try { }
finally
{
rwLock.AcquireWriterLock(Timeout.Infinite);
lockHeld = true;
}
found = dictionary.TryGetValue(key, out inserted);
if (!found)
{
dictionary.Add(key, value);
inserted = value;
}
}
finally
{
if (lockHeld)
{
rwLock.ReleaseWriterLock();
}
}
if (DebugTrace.Verbose && !found)
{
int count = dictionary.Count;
DebugTrace.Trace(TraceLevel.Verbose,
"Added {0} {1} to lookup table. Table contains {2} object{3}",
value.GetType().Name, value,
count, count == 1 ? string.Empty : "s");
}
return inserted;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- RefreshEventArgs.cs
- PersonalizablePropertyEntry.cs
- FilteredXmlReader.cs
- ShaderEffect.cs
- ToolStripDropDownClosedEventArgs.cs
- ManagementDateTime.cs
- BamlStream.cs
- FrugalList.cs
- RenderDataDrawingContext.cs
- ListItem.cs
- SemaphoreFullException.cs
- DataGridLengthConverter.cs
- URL.cs
- EmptyStringExpandableObjectConverter.cs
- TableLayoutStyle.cs
- HtmlTableCell.cs
- MetadataStore.cs
- QuaternionConverter.cs
- TrackingDataItemValue.cs
- _NegoState.cs
- ElementsClipboardData.cs
- TemplateColumn.cs
- isolationinterop.cs
- DesignRelation.cs
- DataKeyArray.cs
- ZoneLinkButton.cs
- KeyValuePair.cs
- FilterQuery.cs
- SqlRewriteScalarSubqueries.cs
- Metadata.cs
- DataException.cs
- TransformPattern.cs
- DelegatingTypeDescriptionProvider.cs
- WebPartDescriptionCollection.cs
- ArrayList.cs
- KeyBinding.cs
- DoubleAnimationUsingPath.cs
- COM2EnumConverter.cs
- QueryPageSettingsEventArgs.cs
- GuidTagList.cs
- SqlDependencyListener.cs
- DataAdapter.cs
- StringFormat.cs
- AsymmetricSecurityProtocol.cs
- Helper.cs
- ProcessHostConfigUtils.cs
- SqlParameter.cs
- Predicate.cs
- StoreContentChangedEventArgs.cs
- RightsManagementUser.cs
- RegistryKey.cs
- BitmapEffect.cs
- NotifyIcon.cs
- NativeMethods.cs
- GeneratedCodeAttribute.cs
- DesignerTransaction.cs
- BatchWriter.cs
- DataGridViewButtonCell.cs
- SystemResources.cs
- VisualStyleElement.cs
- RtfToXamlLexer.cs
- VirtualizedItemProviderWrapper.cs
- MultiByteCodec.cs
- OleServicesContext.cs
- SqlStatistics.cs
- XmlResolver.cs
- ScrollableControlDesigner.cs
- XmlSchemaComplexContentExtension.cs
- DrawingAttributesDefaultValueFactory.cs
- DbMetaDataCollectionNames.cs
- MILUtilities.cs
- LockedBorderGlyph.cs
- X509Certificate2.cs
- XmlDataDocument.cs
- XmlHierarchicalDataSourceView.cs
- TdsParserSafeHandles.cs
- ThaiBuddhistCalendar.cs
- ValidationError.cs
- TextEditorThreadLocalStore.cs
- odbcmetadatacollectionnames.cs
- UserNameSecurityTokenAuthenticator.cs
- ClientSettingsSection.cs
- _NegoStream.cs
- MsmqChannelFactoryBase.cs
- TypeConverterAttribute.cs
- ObjectDataSourceStatusEventArgs.cs
- ToolStripItemImageRenderEventArgs.cs
- PerspectiveCamera.cs
- selecteditemcollection.cs
- ZipIOLocalFileBlock.cs
- SafeRegistryHandle.cs
- Soap.cs
- ToolStripPanelSelectionGlyph.cs
- HScrollBar.cs
- RowTypeElement.cs
- Label.cs
- QilIterator.cs
- MetadataCacheItem.cs
- TaiwanCalendar.cs
- DataStreams.cs