Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6584305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:31:14+00:00 2026-05-25T16:31:14+00:00

I was wondering how TransactionScope class works to keep the transaction between different method

  • 0

I was wondering how TransactionScope class works to keep the transaction between different method calls (without the need to pass it as a parameter) and I came to this doubt. I’ve got two considerations about this question:

1

Looking into TransactionScope’s implementation through Telerik JustDecompile, I’ve found that the current transaction is stored in a ThreadStatic member of the System.Transactions.ContextData class (code below).

internal class ContextData
{
    internal TransactionScope CurrentScope;

    internal Transaction CurrentTransaction;

    internal DefaultComContextState DefaultComContextState;

    [ThreadStatic]
    private static ContextData staticData;

    internal WeakReference WeakDefaultComContext;

    internal static ContextData CurrentData
    {
        get
        {
            ContextData contextDatum = ContextData.staticData;
            if (contextDatum == null)
            {
                contextDatum = new ContextData();
                ContextData.staticData = contextDatum;
            }
            return contextDatum;
        }
    }

    public ContextData()
    {
    }
}

The CurrentData property is called by TransactionScope’s PushScope() method, and the last one is used by most of the TransactionScope constructors.

private void PushScope()
{
    if (!this.interopModeSpecified)
    {
        this.interopOption = Transaction.InteropMode(this.savedCurrentScope);
    }
    this.SetCurrent(this.expectedCurrent);
    this.threadContextData.CurrentScope = this;
}

public TransactionScope(TransactionScopeOption scopeOption)
{
    // ...
    this.PushScope();
    // ...
}

Ok, I guess I’ve found how they do that.

2

I’ve read about how bad is to use ThreadStatic members to store objects within ASP.NET (http://www.hanselman.com/blog/ATaleOfTwoTechniquesTheThreadStaticAttributeAndSystemWebHttpContextCurrentItems.aspx) due the ASP.NET thread switching that might occur, so this data can be lost among the worker threads.

So, it looks like TransactionScope should not work with ASP.NET, right? But as far I have used it on my web applications, I don’t remember any problem that I’ve run into about transaction data being lost.

My question here is “what’s the TransactionScope’s trick to deal with ASP.NET’s thread switching?”.

Did I make a superficial analysis on how TransactionScope stores its transaction objects? Or TransactionScope class wasn’t made to work with ASP.NET, and I can be considered a lucky guy that never had any pain about it?

Could anyone who knows the “very deep buried secrets” of .NET explain that for me?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:31:15+00:00Added an answer on May 25, 2026 at 4:31 pm

    I believe ASP.NET thread switching happens only in specific situations (involving asych IO operations) and early in the request life cycle. Typically, once the control is passed to the actual http handler (for example, Page), thread does not get switched. I believe that in most of situation, transaction scope will get initialized only after that (after page_init/load) and should not be an issue.

    Here are few links that might interest you:

    http://piers7.blogspot.com/2005/11/threadstatic-callcontext-and_02.html

    http://piers7.blogspot.com/2005/12/log4net-context-problems-with-aspnet.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wondering what the difference is between the following: Case 1: Base Class public void
I was wondering how you would use the TransactionScope class in the correct way
Wondering if I need to do something in my swf to be able to
Just wondering what other's thoughts are on using a TransactionScope in an IHttpModule. For
I was wondering if there is a way to have a transaction on object.
wondering what is the major difference between these two: onClick and mouseClick..
Wondering if there’s a plugin or best way of setting up an ActiveRecord class
Wondering what others do / best practice for communicating between layers. This question relates
Just wondering, if I want to create a class that does something and I
Wondering if it's possible to print a carriage return without a line feed in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.