NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Context Programming Tasks

Obtaining the current Context

The property Thread.CurrentContext is used to obtain the current context.

class Thread
{
    //. . .
    public static Context CurrentContext { get {. . .} }
    //. . .
}

Context Local Data Store Allocated Slots

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);

Context Local Data Store Named Slots

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”);

Marking fields as Context Relative Static

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

Instance member field

a and d are instance members.

a. Instance member field. Uninitialized.

d. Instance member field. Initialized.

Class static member field

b. class static member field. Uninitialized.

e. class static member field. Initialized.

The static is unique for the AppDomain.

Context relative static member field

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.

Zero Initialization

Allow only null initialization of context local statics.

Writing your own context attribute

Example MyContextAttribute

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);
   }
   . . . 
};

Example Foo attributed with MyContextAttribute

Shown how a Context-bound class Foo can be attributed with the MyContextAttribute. Note that Foo derives from ContextBoundObject.

[MyContextAttribute()]
public class Foo : ContextBoundObject
{
   . . .
};

Example MySpecializedContextAttribute

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
      . . .
   }
   . . . 
};

Example 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

Example Baz attribute with MySpecializedContextAttribute

Shown how a Context-bound class Baz can be decorated with the MySpecializedContextAttribute.

[MySpecializedContextAttribute()]
public class Baz : ContextBoundObject
{
   . . .
};