The property Thread.CurrentContext is used to obtain the current context.
class Thread { //. . . public static Context CurrentContext { get {. . .} } //. . . }
A data store slot is allocated and returned. The data store slot is used to access the data slot. The data store slot is collected by the GC when all references to it are freed.
Foo foo = new Foo(); // Allocate data slot on all contexts LocalDataStoreSlot dataslot = Context.AllocateDataSlot(); // Store foo in the Context Local Store Context.SetData(dataslot, oFoo); // Retrieve foo from the Context Local Store Foo foo2 = Context.GetData(dataslot);
A slot is allocated using a name and the slot number is returned. The name or the number is used to access the data slot. The slot MUST be freed using the name.
Foo foo = new Foo(); // Allocate data slot on all contexts LocalDataStoreSlot dataslot = Context.AllocateNamedDataSlot(“Foo”); // Store foo in the Context Local Store Context.SetData(dataslot, oFoo); // Retrieve foo from the Context Local Store Foo foo2 = Context.GetData(dataslot); // Free data slot on all contexts Context.FreeNamedDataSlot (“Foo”);
class Foo { int a; static int b; [ContextStatic()] static int c; int d=5; static int e=6; [ContextStatic] static int f=7; Foo() { a = 1; b = 2; c = 3; } void Method1() { Console.WriteLine(“{0}”, a); Console.WriteLine(“{0}”, b); Console.WriteLine(“{0}”, c); Console.WriteLine(“{0}”, d); Console.WriteLine(“{0}”, e ); Console.WriteLine(“{0}”, f ); } }
a and d are instance members.
a. Instance member field. Uninitialized.
d. Instance member field. Initialized.
b. class static member field. Uninitialized.
e. class static member field. Initialized.
The static is unique for the AppDomain.
c. context relative static member field. Uninitialized.
f. context relative static member field. Initialized.
The static is unique for the context. The member field is not shared across contexts.
Allow only null initialization of context local statics.
Shown how new context attributes such MyContextAttribute would look like. Note that MyContextAttribute derives from ContextAttribute. The context attribute implements the IContributeServerContextSink interface.
public class MyContextAttribute : ContextAttribute, IContributeServerContextSink { . . . public IMessageSink GetServerContextSink(IMessageSink nextSink) { return new MyContextServerSink(nextSink); } . . . };
Shown how a Context-bound class Foo can be attributed with the MyContextAttribute. Note that Foo derives from ContextBoundObject.
[MyContextAttribute()] public class Foo : ContextBoundObject { . . . };
Shown how context attributes such MySpecializedContextAttribute would look like. It overrides IContextAttribute.IsContextOK and IContextAttribute.GetPropertyForNewContext methods.
public class MySpecializedContextAttribute : ContextAttribute { . . . public boolean IsContextOK(Context ctx, out Object cookie) { . . . } public IContextProperty GetPropertyForNewContext(Object cookie); { . . . // return an instance of MySpecializedContextProperty . . . } . . . };
Shown how context properties such MySpecializedContextProperty would look like. The context property implements the IContributeServerContextSink interface.
public class MySpecializedContextProperty : IContextProperty, IContributeServerContextSink { . . . public String Name {get {return "MySpecializedContextProperty";} } public boolean IsNewContextOK(Context* newCtx) { . . . } public IMessageSink GetServerContextSink(IMessageSink* nextSink) { . . . } . . . };
IContribute*Sink interfaces | Description |
---|---|
IContributeServerContextSink | Calls into Context (e.g. Synch or Tx) |
IContributeClientContextSink | Calls out of Context (e.g. Sync with reentrancy) |
IContributeObjectSink | Specific object (e.g. JIT Activation) |
IContributeEnvoySink | Project the server context rules as an envoy into the client context |
Shown how a Context-bound class Baz can be decorated with the MySpecializedContextAttribute.
[MySpecializedContextAttribute()] public class Baz : ContextBoundObject { . . . };