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 8457899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:55:54+00:00 2026-06-10T12:55:54+00:00

So here is the question : is there any way to create a Context

  • 0

So here is the question : is there any way to create a Context that will be share only a part of the code. I don’t really know how to explain it, so I will make an example.

Suppose that I have :

using(Context ctx = new Context())
{
    ctx.Set("abc","abc");
    method1();
    method2();
}

and (same for method 2) :

public void method1()
{
    Context ctx = Context.Instance();
    string abc = ctx.Get("abc");
}

The goal is that the Context.Instance() in the methods return the object created in the using but it must remains thread safe. What I mean is that if call is made from outside a using, or inside another using at the same time, it will return the context according to the using. And Context.Instante() will return a new Context if not exist (or null, I will adapt after, that’s not the point).

I know this is not classic way of doing it but I don’t want to have to pass the context at each call. The real method will have lots of subs methods and only the lasts will need it, but it must be common since the beginning.

The goal is to create one transaction that will be shared to all (and only) submethods, but the top methods don’t have access to EF classes. The only solution I have is to use the session to store my EF Context, or in static for batches since they don’t have session. That’s my goal, but the problem is much more generic.

I hope I’m clear, don’t hesitate to ask questions. And add tags since I don’t really know what tags add.

Thanks !

EDIT

I might not have been clear enough. Singleton is not enough, the static is shared between all the instance. If two call to the using are done in the same time by two different user, the Context will be the same to them, but I want them different.

Ideally, I would like to allow things like :

using(Context ctx = new Context())
{
    ctx.Set("abc","abc");
    method1();
    using(Context ctx = new Context())
    {
        ctx.Set("abc","def");
        method2();
    }
    method1();
}

But I think I ask too much.

  • 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-06-10T12:55:55+00:00Added an answer on June 10, 2026 at 12:55 pm

    Have your Context.Instance() method manage a thread static variable. The first call can create the instance, other calls can use it, and at the end of the using of the first call, you destroy it.

    Using the thread-static approach, no multi threading problems should arise, since each thread has its own instance. In a single thread, every ‘next method execution’ will use the same instance.

    UPDATE:

    Some code. If I debug through it, only a single context is created, the second using statement receives the same context as the first using statement.

    class Context : IDisposable {
        [ThreadStatic]
        private static Context _instance;
        [ThreadStatic]
        private static int _instanceCounter;
    
        public static Context Instance() {
            if (_instanceCounter == 0) {
                _instance = new Context();
            }
            _instanceCounter++;
            return _instance;
        }
    
        private static void Release() {
            _instanceCounter--;
            if (_instanceCounter == 0) {
                if (_instance != null)
                    _instance.Dispose();
                _instance = null;
            }
        }
    
        public void Dispose() {
            Release();
        }
    }
    
    public class Test {
        public void Test1() {
            using (var context = Context.Instance()) {
                // do something
                Test2();
            }
        }
    
        private void Test2() {
            using (var context = Context.Instance()) {
                // identical context as in Test1
                // do something
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple question here: is there any way to convert from a jagged array to
In Java, is there any way to create an instance of any class that
Is there any way that you may create a puzzle of three pieces with
Note: There was not any question with this kind of problem here or anywhere...
a simple question here Is there a way to change the text click here
There is a question here in stackoverflow with the same title but that is
I am aware that there is a similar question here with no solution. I'm
Here is my question - There are two classes Order and PaymentType . An
I see there is a question here but there is no definite answer. Has
There is a similar question here on SO, but I have a fairly small

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.