Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

wcf

While my service executes, many classes will need to access User.Current (that is my own User class). Can I safely store _currentUser in a [ThreadStatic] variable? Does WCF reuse its threads? If that is the case, when will it clean-up the ThreadStatic data? If using ThreadStatic is not safe, where should I put that data? Is there a place inside OperationContext.Current where I can store that kind of data?

Edit 12/14/2009: I can assert that using a ThreadStatic variable is not safe. WCF threads are in a thread pool and the ThreadStatic variable are never reinitialized.

like image 956
Sylvain Avatar asked Dec 13 '09 06:12

Sylvain


2 Answers

There's a blog post which suggests implementing an IExtension<T>. You may also take a look at this discussion.

Here's a suggested implementation:

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) { }
}

Which you could use like that:

WcfOperationContext.Current.Items["user"] = _currentUser;
var user = WcfOperationContext.Current.Items["user"] as MyUser;
like image 185
Darin Dimitrov Avatar answered Nov 05 '22 23:11

Darin Dimitrov


An alternative solution without adding extra drived class.

    OperationContext operationContext = OperationContext.Current;
    operationContext.IncomingMessageProperties.Add("SessionKey", "ABCDEFG");

To get the value

var ccc = aaa.IncomingMessageProperties["SessionKey"];

That's it

like image 1
ValidfroM Avatar answered Nov 06 '22 00:11

ValidfroM