logo
down
shadow

Is it safe to use ThreadStatic variable in WCF?


Is it safe to use ThreadStatic variable in WCF?

By : Afiqruzaini Mohsin
Date : January 02 2021, 06:48 AM
I wish did fix the issue. I would recommend you to use OperationContext. Here's an example of how you could use it to persist some state.
code :


Share : facebook icon twitter icon
Is this a thread safe way to initialize a [ThreadStatic]?

Is this a thread safe way to initialize a [ThreadStatic]?


By : Clint Rowden
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , If its ThreadStatic there's one copy per thread. So, by definition, its thread safe.
This blog has some good info on ThreadStatic.
Where to store data for current WCF call? Is ThreadStatic safe?

Where to store data for current WCF call? Is ThreadStatic safe?


By : vrutika changela
Date : March 29 2020, 07:55 AM
will be helpful for those in need There's a blog post which suggests implementing an IExtension. You may also take a look at this discussion.
Here's a suggested implementation:
code :
public class WcfOperationContext : IExtension<OperationContext>
{
    private readonly IDictionary<string, object> items;

    private WcfOperationContext()
    {
        items = new Dictionary<string, object>();
    }

    public IDictionary<string, object> Items
    {
        get { return items; }
    }

    public static WcfOperationContext Current
    {
        get
        {
            WcfOperationContext context = OperationContext.Current.Extensions.Find<WcfOperationContext>();
            if (context == null)
            {
                context = new WcfOperationContext();
                OperationContext.Current.Extensions.Add(context);
            }
            return context;
        }
    }

    public void Attach(OperationContext owner) { }
    public void Detach(OperationContext owner) { }
}
WcfOperationContext.Current.Items["user"] = _currentUser;
var user = WcfOperationContext.Current.Items["user"] as MyUser;
VB.NET 4.0: ThreadStatic doesn't appear to be thread safe for my TdConnection property

VB.NET 4.0: ThreadStatic doesn't appear to be thread safe for my TdConnection property


By : Lev Rubel
Date : March 29 2020, 07:55 AM
I wish did fix the issue. You should have one connection per request instead of one per thread.
To do that, store it in HttpContext.Current.Items instead of the ThreadStatic field.
Access ThreadStatic - Variable from outside of the thread

Access ThreadStatic - Variable from outside of the thread


By : Looly
Date : March 29 2020, 07:55 AM
seems to work fine My understanding is that ThreadStatic is always relative to the thread. You could get this info out if you instruct the thread to read is for you.
You could also make your static value an object, then have that thread add the reference to a central location to be monitored. You would still have the issues of making sure that changes to that object get periodically synced or set it to volatile.
Viewing ThreadStatic variable from seperate class

Viewing ThreadStatic variable from seperate class


By : user3009303
Date : March 29 2020, 07:55 AM
it fixes the issue The ThreadStatic variable is a static variable that you can access just like any static variable, i.e. MainForm._skipCurrent. However, the ThreadStatic variable will not achieve what you want. Each thread has its own copy of a ThreadStatic variable to work with. So, if the user clicks a Cancel button in the UI thread and this sets MainForm._skipCurrent to true, the thread executing Start() will not see that value.
What you want is to use an ordinary static variable.
shadow
Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk