Skip to content

Grain

class

Namespace: Orleans

The abstract base class for all grain classes.
public abstract class Grain : IGrainBase, IAddressable

Constructors

  • Grain This constructor should never be invoked. We expose it so that client code (subclasses of Grain) do not have to add a constructor. Client code should use the GrainFactory property to get a reference to a Grain.
  • Grain(IGrainContext, IGrainRuntime?) Grain implementers do NOT have to expose this constructor but can choose to do so. This constructor is particularly useful for unit testing where test code can create a Grain and replace the IGrainIdentity and IGrainRuntime with test doubles (mocks/stubs).

Properties

  • GrainContext Gets the runtime context for this grain activation.
  • GrainFactory Gets an object which can be used to access other grains. Null if this grain is not associated with a Runtime, such as when created directly for unit testing.
  • GrainReference Gets a reference to this grain.
  • IdentityString String representation of grain's SiloIdentity including type and primary key.
  • RuntimeIdentity A unique identifier for the current silo. There is no semantic content to this string, but it may be useful for logging.
  • ServiceProvider Gets the IServiceProvider managed by the runtime. Null if this grain is not associated with a Runtime, such as when created directly for unit testing.

Methods

  • DeactivateOnIdle Deactivate this activation of the grain after the current grain method call is completed. This call will mark this activation of the current grain to be deactivated and removed at the end of the current method. The next call to this grain will result in a different activation to be used, which typical means a new activation will be created automatically by the runtime.
  • DelayDeactivation(TimeSpan) Delay Deactivation of this activation at least for the specified time duration.

    A positive timeSpan value means “prevent GC of this activation for that time span”.

    A System.TimeSpan.Zero value means “cancel the previous setting of the Grain call and make this activation behave based on the regular Activation Garbage Collection settings”.

    A System.Threading.Timeout.InfiniteTimeSpan value means “delay deactivation indefinitely”.

    Grain would undo / override any current “keep alive” setting, making this grain immediately available for deactivation.

  • MigrateOnIdle Starts an attempt to migrating this instance to another location. Migration captures the current RequestContext, making it available to the activation's placement director so that it can consider it when selecting a new location. Migration will occur asynchronously, when no requests are executing, and will not occur if the activation's placement director does not select an alternative location.
  • OnActivateAsync(CancellationToken) This method is called at the end of the process of activating a grain. It is called before any messages have been dispatched to the grain. For grains with declared persistent state, this method is called after the State property has been populated.
  • OnDeactivateAsync(DeactivationReason, CancellationToken) This method is called at the beginning of the process of deactivating a grain.
  • RegisterTimer(Func<object?, Task>, object?, TimeSpan, TimeSpan) Registers a timer to send periodic callbacks to this grain.