//--------------------------------------------------------------------- // Public Cadence workflow related operations. /// <summary> /// Registers a workflow implementation with Cadence. /// </summary> /// <typeparam name="TWorkflow">The <see cref="IWorkflowBase"/> derived class implementing the workflow.</typeparam> /// <param name="workflowTypeName"> /// Optionally specifies a custom workflow type name that will be used /// for identifying the workflow implementation in Cadence. This defaults /// to the fully qualified <typeparamref name="TWorkflow"/> type name. /// </param> /// <param name="domain">Optionally overrides the default client domain.</param> /// <returns>The tracking <see cref="Task"/>.</returns> /// <exception cref="InvalidOperationException">Thrown if another workflow class has already been registered for <paramref name="workflowTypeName"/>.</exception> /// <exception cref="CadenceWorkflowWorkerStartedException"> /// Thrown if a workflow worker has already been started for the client. You must /// register activity workflow implementations before starting workers. /// </exception> /// <remarks> /// <note> /// Be sure to register all of your workflow implementations before starting a workflow worker. /// </note> /// </remarks> public async Task RegisterWorkflowAsync <TWorkflow>(string workflowTypeName = null, string domain = null) where TWorkflow : WorkflowBase { CadenceHelper.ValidateWorkflowImplementation(typeof(TWorkflow)); CadenceHelper.ValidateWorkflowTypeName(workflowTypeName); if (string.IsNullOrEmpty(workflowTypeName)) { workflowTypeName = workflowTypeName ?? typeof(TWorkflow).FullName; } if (workflowWorkerStarted) { throw new CadenceWorkflowWorkerStartedException(); } // We need to register a workflow type name for each workflow method, // appending the method type separator and method name for workflow // methods that specify names. if (!WorkflowBase.Register(this, typeof(TWorkflow), workflowTypeName)) { var reply = (WorkflowRegisterReply) await CallProxyAsync( new WorkflowRegisterRequest() { Name = workflowTypeName, Domain = ResolveDomain(domain) }); reply.ThrowOnError(); } }
/// <summary> /// Scans the assembly passed looking for workflow implementations derived from /// <see cref="WorkflowBase"/> and tagged by <see cref="WorkflowAttribute"/> with /// <see cref="WorkflowAttribute.AutoRegister"/> set to <c>true</c> 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="WorkflowAttribute"/> that are not /// derived from <see cref="WorkflowBase"/>. /// </exception> /// <exception cref="InvalidOperationException">Thrown if one of the tagged classes conflict with an existing registration.</exception> /// <exception cref="CadenceWorkflowWorkerStartedException"> /// Thrown if a workflow worker has already been started for the client. You must /// register activity workflow implementations before starting workers. /// </exception> /// <remarks> /// <note> /// Be sure to register all of your workflow implementations before starting a workflow worker. /// </note> /// </remarks> public async Task RegisterAssemblyWorkflowsAsync(Assembly assembly) { Covenant.Requires <ArgumentNullException>(assembly != null); if (workflowWorkerStarted) { throw new CadenceWorkflowWorkerStartedException(); } foreach (var type in assembly.GetTypes()) { var workflowAttribute = type.GetCustomAttribute <WorkflowAttribute>(); if (workflowAttribute != null) { if (type.BaseType == typeof(WorkflowBase)) { if (workflowAttribute.AutoRegister) { var workflowTypeName = workflowAttribute.TypeName ?? type.FullName; if (!WorkflowBase.Register(this, type, workflowTypeName)) { var reply = (WorkflowRegisterReply) await CallProxyAsync( new WorkflowRegisterRequest() { Name = workflowTypeName }); reply.ThrowOnError(); } } } else { throw new TypeLoadException($"Type [{type.FullName}] is tagged by [{nameof(WorkflowAttribute)}] but is not derived from [{nameof(WorkflowBase)}]."); } } } }