예제 #1
0
        /// <summary>
        /// Returns a reference to the provided grain.
        /// </summary>
        /// <param name="grain">The grain to create a reference for.</param>
        /// <returns>A reference to the provided grain.</returns>
        internal static GrainReference AsReference(this IAddressable grain)
        {
            if (grain is null)
            {
                ThrowGrainNull();
            }

            // When called against an instance of a grain reference class, do nothing
            var reference = grain as GrainReference;

            if (reference != null)
            {
                return(reference);
            }

            var context = grain switch
            {
                Grain grainBase => grainBase.GrainContext,
                IGrainBase activation => activation.GrainContext,
                ISystemTargetBase systemTarget => systemTarget,
                _ => throw new ArgumentException(GetWrongGrainTypeErrorMessage(grain), nameof(grain))
            };

            if (context?.GrainReference is not {
            } grainRef)
            {
                throw new ArgumentException(WRONG_GRAIN_ERROR_MSG, nameof(grain));
            }

            return(grainRef);
        }
예제 #2
0
        public static IStreamProvider GetStreamProvider(this IGrainBase grain, string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            return(grain.GrainContext.ActivationServices.GetRequiredServiceByName <IStreamProvider>(name));
        }
예제 #3
0
        /// <summary>
        /// Returns the grain id corresponding to the provided grain.
        /// </summary>
        /// <param name="grain">The grain</param>
        /// <returns>The grain id corresponding to the provided grain.</returns>
        /// <exception cref="ArgumentException">The provided value has the wrong type or has no id.</exception>
        public static GrainId GetGrainId(this IAddressable grain)
        {
            var grainId = grain switch
            {
                Grain grainBase => grainBase.GrainId,
                GrainReference grainReference => grainReference.GrainId,
                IGrainBase grainActivation => grainActivation.GrainContext.GrainId,
                ISystemTargetBase systemTarget => systemTarget.GrainId,
                _ => throw new ArgumentException(GetWrongGrainTypeErrorMessage(grain), nameof(grain))
            };

            if (grainId.IsDefault)
            {
                throw new ArgumentException(WRONG_GRAIN_ERROR_MSG, nameof(grain));
            }

            return(grainId);
        }
예제 #4
0
 /// <summary>
 /// 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.
 /// </summary>
 public static void DeactivateOnIdle(this IGrainBase grain) =>
 grain.GrainContext.Deactivate(new(DeactivationReasonCode.ApplicationRequested, $"{nameof(DeactivateOnIdle)} was called."));