The framework does not seem to allow this.
Perhaps we could discuss why you need to know this information? TransactionScopeOptions gives you some flexibility as to when to create transactions.
However, abandoning the no answer, I looked at the source a bit later, and I created this code that works. Please note that this code may stop functioning at any time with corrections in the framework !!!!
static bool IsEnlisted(SqlConnection sqlConnection)
{
object innerConnection = typeof(SqlConnection).GetField("_innerConnection", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).GetValue(sqlConnection);
var enlistedTransactionField =
EnumerateInheritanceChain(innerConnection.GetType())
.Select(t => t.GetField("_enlistedTransaction", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy))
.Where(fi => fi != null)
.First();
object enlistedTransaction = enlistedTransactionField.GetValue(innerConnection);
return enlistedTransaction != null;
}
static IEnumerable<Type> EnumerateInheritanceChain(Type root)
{
for (Type current = root; current != null; current = current.BaseType)
yield return current;
}
Again, this is the use of private variables and inner classes within .NET. Although he works today, he may not tomorrow.
source
share