DEV Community

Silvan
Silvan

Posted on • Updated on

Using Camunda with .NET

This is a tutorial how to interacte with Camunda BPM within a .NET Core application. Camunda BPM is an open source workflow engine written in Java. The engine offers a Java-API, but also a very good documentated REST-API. You can implement your own REST-Client or generate it from the provided OpenApi description. The Camunda.OpenApi.Client Nuget package includes a pre-buildet generated Camunda REST-Client from the OpenApi description. You don't have to take care about the generatation of the REST-Client, you can just use it. You can have a look at the Github Repo to see how the generatation of the REST-Client works.

You can add the Nuget Package to your project:

Install-Package Camunda.OpenApi.Client -Version 1.0.1

Enter fullscreen mode Exit fullscreen mode

After that you can define your own extension to add this later over Dependency-Injection:

public static class CamundaClientExtension
    {
        public static void AddCamundaClient(this IServiceCollection services)
        {
            var config = new Configuration
            {
                BasePath = "http://localhost:8080/engine-rest",
                UserAgent = "CamundaClient"
            };

            // add all needed api's
            services.AddSingleton(new TaskApi(config));
            services.AddSingleton(new TaskVariableApi(config));
            services.AddSingleton(new ProcessInstanceApi(config));
            services.AddSingleton(new ProcessDefinitionApi(config));
            services.AddSingleton(new DeploymentApi(config));
            services.AddSingleton(new MessageApi(config));
            services.AddSingleton(new HistoricProcessInstanceApi(config));
            services.AddSingleton(new HistoricActivityInstanceApi(config));
        }
    }
Enter fullscreen mode Exit fullscreen mode

Add services.AddCamundaClient(); in Startup.cs on the ConfigureServices method:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddCamundaClient();
    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new OpenApiInfo {Title = "TestWebApp", Version = "v1"});
    });
}

Enter fullscreen mode Exit fullscreen mode

Now you can inject the api services everywhere in your application. For example in a ApiController:

[ApiController]
[Route("[controller]")]
public class ExampleController : ControllerBase
{
    private readonly TaskApi _taskApi;

    public ExampleController(TaskApi taskApi)
    {
        _taskApi = taskApi;
    }

    [HttpGet]
    public IEnumerable<TaskDto> Get()
    {
        return _taskApi.QueryTasks();
    }
}
Enter fullscreen mode Exit fullscreen mode

Discussion (3)

Collapse
vberen profile image
Nicklas Wessel • Edited on

It would be helpful if you could introduce what it is in the beginning.
I'm still clueless to what this package can do for me or what it is at all :)

Also, you have just copy/pasted everything from github. I'm confused

Collapse
silvanbrenner profile image
Silvan Author

The idea is that not everyone need to care about generating the REST-Client itself. So you can just use the pre-buildet Nuget package. And yes it's more or less the same as on github. This blog post should be a oppertunity for me to share my project further.

Collapse
vberen profile image
Nicklas Wessel

I think it is fine to share your project.
But i think it would be good to explain what Camunda is, or link to their page, since i thought your project was Camunda, and I'm still not sure what problem it solves.

We need some background :) the project looks cool though