Пример #1
0
        /// <summary>
        /// Scans the assembly passed looking for activity implementations derived from
        /// <see cref="ActivityBase"/> and tagged by <see cref="ActivityAttribute"/> and
        /// registers them with Temporal.
        /// </summary>
        /// <param name="assembly">The target assembly.</param>
        /// <param name="disableDuplicateCheck">Disable checks for duplicate activity registrations.</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="ActivityBase"/>.
        /// </exception>
        /// <exception cref="InvalidOperationException">Thrown if one of the tagged classes conflict with an existing registration.</exception>
        /// <exception cref="InvalidOperationException">
        /// Thrown if the worker has already been started.  You must register workflow
        /// and activity implementations before starting workers.
        /// </exception>
        /// <exception cref="RegistrationException">Thrown when there's a problem with the registration.</exception>
        /// <remarks>
        /// <note>
        /// Be sure to register all services you will be injecting into activities via
        /// <see cref="NeonHelper.ServiceContainer"/> before you call this as well as
        /// registering of your activity implementations before starting a worker.
        /// </note>
        /// </remarks>
        public async Task RegisterAssemblyActivitiesAsync(Assembly assembly, bool disableDuplicateCheck = false)
        {
            await SyncContext.Clear;

            Covenant.Requires <ArgumentNullException>(assembly != null, nameof(assembly));
            EnsureNotDisposed();
            EnsureCanRegister();

            lock (registeredActivityTypes)
            {
                foreach (var activityType in assembly.GetTypes().Where(type => type.IsClass))
                {
                    var activityAttribute = activityType.GetCustomAttribute <ActivityAttribute>();

                    if (activityAttribute != null && activityAttribute.AutoRegister)
                    {
                        var activityTypeName = TemporalHelper.GetActivityTypeName(activityType, activityAttribute);

                        if (registeredActivityTypes.Contains(activityType))
                        {
                            if (disableDuplicateCheck)
                            {
                                continue;
                            }
                            else
                            {
                                throw new RegistrationException($"Activity implementation [{activityType.FullName}] has already been registered.");
                            }
                        }

                        registeredActivityTypes.Add(activityType);
                    }
                }
            }
        }