Grain
class
Namespace: Orleans
The abstract base class for all grain classes.
public abstract class Grain : IGrainBase, IAddressableConstructors
GrainThis 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
GrainContextGets the runtime context for this grain activation.GrainFactoryGets 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.GrainReferenceGets a reference to this grain.IdentityStringString representation of grain's SiloIdentity including type and primary key.RuntimeIdentityA unique identifier for the current silo. There is no semantic content to this string, but it may be useful for logging.ServiceProviderGets 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
DeactivateOnIdleDeactivate 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
timeSpanvalue means “prevent GC of this activation for that time span”.A
System.TimeSpan.Zerovalue means “cancel the previous setting of theGraincall and make this activation behave based on the regular Activation Garbage Collection settings”.A
System.Threading.Timeout.InfiniteTimeSpanvalue means “delay deactivation indefinitely”.Grainwould undo / override any current “keep alive” setting, making this grain immediately available for deactivation.MigrateOnIdleStarts an attempt to migrating this instance to another location. Migration captures the currentRequestContext, 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.
