WCF infrastructure allows you to store context sensitive data in InstanceContext of the service instance. For that you should implement from IExtension<InstanceContext> and plug that class into WCF’s infrastructure in any of available ways.
When I worked on a class that can store contextual information in Web context or WCF context depending on some configuration parameters, I preferred to have similar idioms, and I wrote an HttpContext-like class for WCF.
///<summary> /// This class incapsulates context information for a service instance ///</summary> public class WcfInstanceContext : IExtension<InstanceContext> { private readonly IDictionary items; private WcfInstanceContext() { items = new Hashtable(); } ///<summary> /// <see cref="IDictionary"/> stored in current instance context. ///</summary> public IDictionary Items { get { return items; } } ///<summary> /// Gets the current instance of <see cref="WcfInstanceContext"/> ///</summary> public static WcfInstanceContext Current { get { WcfInstanceContext context = OperationContext.Current.InstanceContext.Extensions.Find<WcfInstanceContext>(); if (context == null) { context = new WcfInstanceContext(); OperationContext.Current.InstanceContext.Extensions.Add(context); } return context; } } /// <summary> /// <see cref="IExtension{T}"/> Attach() method /// </summary> public void Attach(InstanceContext owner) { } /// <summary> /// <see cref="IExtension{T}"/> Detach() method /// </summary> public void Detach(InstanceContext owner) { } }
Now, you can use this class to store and retrieve data in the same manner as you’re working with HttpContext:
WcfInstanceContext.Current.Items["key"] = new MyClass(); MyClass myClass = WcfInstanceContext.Current.Items["key"] as MyClass;
Of course, when doing this you should be inside of WCF session…
Enjoy!

5 comments
Comments feed for this article
March 19, 2009 at 13:14
shlomi
What would you do if your WCF service is singleton?
(the items remains in the dictionary regardsless of the WCF request context, as in HttpContext).
Thanks,
Shlomi
March 19, 2009 at 13:31
shlomi
Found the solution – instead of using InstanceContext extension – use OperationContext extension. I think this is also more appropriate to what you meant.
public class WcfOperationContext : IExtension
{
private readonly IDictionary items;
private WcfOperationContext()
{
items = new Hashtable();
}
public IDictionary Items
{
get { return items; }
}
public static WcfOperationContext Current
{
get
{
WcfOperationContext context = OperationContext.Current.Extensions.Find();
if (context == null)
{
context = new WcfOperationContext();
OperationContext.Current.Extensions.Add(context);
}
return context;
}
}
public void Attach(OperationContext owner) { }
public void Detach(OperationContext owner) { }
}
March 19, 2009 at 13:42
Valeriu Caraulean
I haven’t working with WCF for a while and I don’t remember details, but probably you’re right…
April 18, 2009 at 17:35
vince
Ok, the context works. But how do you persist the data between calls to the service. Items are cleared on each call. I have no idea how this session works on WCF.
April 18, 2009 at 21:00
Valeriu Caraulean
@vince
For us, the anwer was “you don’t”.
We don’t keep the state between calls. Our services are steless. And, so far, we’re happy with it…
Back to keeping the state, if your serivice is instantiated “Per Call” you will not have any context between calls. But you can use “Per Session” mode on your services. This way, between calls you’ll have same InstanceContext (and/or the OperationContext, I don’t remember now, sorry).
But if your question was really about “persisting” data to some durable storage, the database does it well.