Ejemplo n.º 1
0
        //---------------------------------------------------------------------
        // Cadence activity related operations.

        /// <summary>
        /// Registers an activity implementation with Cadence.
        /// </summary>
        /// <typeparam name="TActivity">The <see cref="IActivityBase"/> derived class implementing the activity.</typeparam>
        /// <param name="activityTypeName">
        /// Optionally specifies a custom activity type name that will be used
        /// for identifying the activity implementation in Cadence.  This defaults
        /// to the fully qualified <typeparamref name="TActivity"/> type name.
        /// </param>
        /// <returns>The tracking <see cref="Task"/>.</returns>
        /// <exception cref="InvalidOperationException">Thrown if a different activity class has already been registered for <paramref name="activityTypeName"/>.</exception>
        /// <exception cref="CadenceActivityWorkerStartedException">
        /// Thrown if an activity worker has already been started for the client.  You must
        /// register activity implementations before starting workers.
        /// </exception>
        /// <remarks>
        /// <note>
        /// Be sure to register all of your activity implementations before starting a workflow worker.
        /// </note>
        /// </remarks>
        public async Task RegisterActivityAsync <TActivity>(string activityTypeName = null)
            where TActivity : ActivityBase
        {
            CadenceHelper.ValidateActivityImplementation(typeof(TActivity));

            if (string.IsNullOrEmpty(activityTypeName))
            {
                activityTypeName = activityTypeName ?? typeof(TActivity).FullName;
            }

            if (activityWorkerStarted)
            {
                throw new CadenceActivityWorkerStartedException();
            }

            if (!ActivityBase.Register(this, typeof(TActivity), activityTypeName))
            {
                var reply = (ActivityRegisterReply) await CallProxyAsync(
                    new ActivityRegisterRequest()
                {
                    Name = activityTypeName
                });

                reply.ThrowOnError();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Scans the assembly passed looking for activity implementations derived from
        /// <see cref="IActivityBase"/> and tagged by <see cref="ActivityAttribute"/> and
        /// registers them with Cadence.
        /// </summary>
        /// <param name="assembly">The target assembly.</param>
        /// <returns>The tracking <see cref="Task"/>.</returns>
        /// <exception cref="TypeLoadException">
        /// Thrown for types tagged by <see cref="ActivityAttribute"/> that are not
        /// derived from <see cref="IActivityBase"/>.
        /// </exception>
        /// <exception cref="InvalidOperationException">Thrown if one of the tagged classes conflict with an existing registration.</exception>
        /// <exception cref="CadenceActivityWorkerStartedException">
        /// Thrown if an activity worker has already been started for the client.  You must
        /// register activity implementations before starting workers.
        /// </exception>
        /// <remarks>
        /// <note>
        /// Be sure to register all of your activity implementations before starting a workflow worker.
        /// </note>
        /// </remarks>
        public async Task RegisterAssemblyActivitiesAsync(Assembly assembly)
        {
            Covenant.Requires <ArgumentNullException>(assembly != null);

            if (activityWorkerStarted)
            {
                throw new CadenceActivityWorkerStartedException();
            }

            foreach (var type in assembly.GetTypes())
            {
                var activityAttribute = type.GetCustomAttribute <ActivityAttribute>();

                if (activityAttribute != null)
                {
                    if (type.Implements <IActivityBase>())
                    {
                        if (activityAttribute.AutoRegister)
                        {
                            var activityTypeName = activityAttribute.TypeName ?? type.FullName;

                            if (!ActivityBase.Register(this, type, activityTypeName))
                            {
                                var reply = (ActivityRegisterReply) await CallProxyAsync(
                                    new ActivityRegisterRequest()
                                {
                                    Name = activityTypeName
                                });

                                reply.ThrowOnError();
                            }
                        }
                    }
                    else
                    {
                        throw new TypeLoadException($"Type [{type.FullName}] is tagged by [{nameof(ActivityAttribute)}] but is not derived from [{nameof(IActivityBase)}].");
                    }
                }
            }
        }