TransactionScope Debugger behavior
I found a strange behavior when debugging code that involved TransactionScope:
var sqlCommand = new SqlCommand(); using (var ts = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted })) { sqlCommand.CommandText = "INSERT INTO TestTable1..."; sqlCommand.ExecuteNonQuery(); sqlCommand.CommandText = "INSERT INTO TestTable2..."; sqlCommand.ExecuteNonQuery(); ts.Complete(); }
by placing a break point where the TransactionScope instance is created in the using statement and hovering mouse pointer over the word 'new' I get a debugger Tooltip with information about a TransactionScope which seems to create an actual instance of TransactionScope. Once I attempt to step through the code and the program attempts to create the TransactionScope an Exception is thrown with the following text:
The transaction specified for TransactionScope has a different IsolationLevel than the value requested for the scope. Parameter name: transactionOptions.IsolationLevel
It may also seem that the Debugger Tooltip might cause unintended behavior if it is creating instances of objects during debugging when a developer might not expect it to.
cc: @gregg-miskelly
Gregg, is it expected that hovering over "new" in the debugger will force an allocation of the associated object and any side effects that ctor may have? Is the expectation that the ctor would use Debugger.NotifyOfCrossThreadDependency() to prevent the execution in the debugger?
cc: @KevinH-MS @Pnelsonmsft
@stephentoub to me it seems like 'new
Yes, the NotifyOfCrossThreadDependency behavior is definitely broken here. I opened VS bug 259670 to track this.
Yes, the NotifyOfCrossThreadDependency behavior is definitely broken here
Sorry, just to clarify, TransactionScope is not currently using NotifyOfCrossThreadDependency. I was asking if that is the recommended way to fix this if the VS behavior doesn't change, whether NotifyOfCrossThreadDependency should be put into such ctors. Seems though like it'd be better to change the VS behavior rather than putting NOCTD into any ctor that may have side effects.
@gregg-miskelly Yeah, it seems to me like we should block evaluation of constructor calls in the DataTip language service.
@KevinH-MS we could certainly block in in the language service too, but it seems like the EE should be treating constructor calls like other non-property method calls and returning PotentialSideEffect.
I opened https://github.com/dotnet/roslyn/issues/13526 for this.
@stephentoub I don't think there is any need to use NotifyOfCrossThreadDependency unless you think for some reason people might accidentally explicitly evaluate new TransactionScope(...) in the watch window. The data tip case should be blocked.
@gregg-miskelly I think that's arguably correct (that PotentialSideEffect could be set), but I wonder if it would get annoying in the Watch window case...
CC @cston for thoughts...
Where do I find information about "PotentialSideEffect"? Searching for that string in the repo code isn't finding anything.
@jimcarley this isn't a CoreFX concept. This is how the C# expression compiler tells the debugger that a given expression shouldn't be evaluated in situations that are more sensitive (data tips, automatic re-evaluation of properties). Ex: here is where the C# expression compiler sets this for assignment statements -- https://github.com/dotnet/roslyn/blob/614299ff83da9959fa07131c6d0ffbc58873b6ae/src/ExpressionEvaluator/CSharp/Source/ExpressionCompiler/CompilationContext.cs#L233
I think you can resolve this issue as external since it is tracked with roslyn#13526.
Okay. While I don't completely understand how PotentialSideEffect would be used in this TransactionScope scenario, it does seem to me to be external to System.Transactions. I am going to go ahead and resolve this issue.
External issue. Please see https://github.com/dotnet/roslyn/issues/13526