Example of IActivityTypeProvider based on a single type
See original GitHub issueHi,
I’ve a situation where I have a swagger.json file and I want to create an activity for each API call inside swagger file. I created a based activity that accept paramters and is able to do the call.
I’ve created a class that implements IActivityTypeProvider and if I return only one class everything seems to work fine, but when I return a second ActivityDefinition based on the same underling class, I got error because elsa is trying to register multiple time the same activity.
I’ve tried to make the class generic to have a single runtime type for each activity but then I have error like following one and the workflow does not works.
Is there an example that shows how to use a base class to dynamically generates multiple activity definition at runtime? Is this even a supported scenario?
Thanks for any information you can give me.
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
2022-07-27 13:12:22 [ERR] Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware | An unhandled exception has occurred while executing the request.
Elsa.Exceptions.WorkflowException: The activity type 'Activity' has not been registered. Did you forget to register it with ElsaOptions?
at Elsa.Services.Workflows.ActivityTypeService.GetActivityTypeAsync(String type, CancellationToken cancellationToken)
at Elsa.Scripting.JavaScript.Handlers.RenderJavaScriptTypeDefinitions.<>c__DisplayClass3_0.<<Handle>b__5>d.MoveNext()
--- End of stack trace from previous location ---
at Elsa.Scripting.JavaScript.Handlers.RenderJavaScriptTypeDefinitions.Handle(RenderingTypeScriptDefinitions notification, CancellationToken cancellationToken)
at MediatR.Mediator.PublishCore(IEnumerable`1 allHandlers, INotification notification, CancellationToken cancellationToken)
at Elsa.Scripting.JavaScript.Providers.DotNetTypeScriptDefinitionProvider.GenerateTypeScriptDefinitionsAsync(StringBuilder builder, ICollection`1 declaredTypes, WorkflowDefinition workflowDefinition, IntellisenseContext context, CancellationToken cancellationToken)
at Elsa.Scripting.JavaScript.Services.TypeScriptDefinitionService.GenerateTypeScriptDefinitionsAsync(WorkflowDefinition workflowDefinition, IntellisenseContext context, CancellationToken cancellationToken)
at Elsa.Server.Api.Endpoints.Scripting.JavaScript.TypeDefinitions.Get.Handle(String workflowDefinitionId, IntellisenseContext context, Nullable`1 version, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
Issue Analytics
- State:
- Created a year ago
- Comments:7 (7 by maintainers)
Found My error, in activity activation I used this piece of code I’ve found on some issue
And this generates all sort of problem, I’ve changed as for your example to
And everything works as a charm.
I’m still evaluating if code generation is a better approach (lots of flexibility) but now I have two distinct strategies to generate actions.
My approach is SUPER SIMPLE, I simply take swagger json definition, parse, then generates the activities. I need this approach because we have some specific API behaviour like bearer tokens, special return types, etc. With this approach I have full intellisense 😃
If you are insterested, feel free to contact me on my private mail alkampfer@nablasoft.com and I’ll check what details I can share with you.
You have several options, I’m talking about code generation at compile time, it is really simple and you can simply work with text template where you can simply replace strings and inject in compilation pipeline.
There are various way to obtain this, one is to let NSwag generate a C# client for you, then copy C# code inside your project and let code generator inspect that code and then generate classes.
Or you can simply include json file and let generator work with JSON file.
Actually I’m using an activity provider, because I’ve found problem on my code, code generation can give you more flexibility but is indeed an extra layer of complexity 😃 but I can now choose the technique that suites me most.
Thanks for the help.