This guide explains how to setup a csharp project to automate a process using Camunda Platform 8.
For the complete code see the Program.cs
file. You can run it using the following command.
dotnet run
The open source library
zeebe-client-csharp
provides a Zeebe client. The nuget package is called zb-client
and can be found here.
If we want to connect to a Camunda Platform 8 SaaS cluster we need the contact point, also know as ZEEBE_ADDRESS
.
Furthermore we need the clientId
and clientSecret
all these information we get from the client credentials.
The credentails can be specified in the client builder.
IZeebeClient zeebeClient = CamundaCloudClientBuilder.Builder()
.UseClientId("kYbf5pwF3WP_ZuOU_13fde~uG7M_IaGZ")
.UseClientSecret("1nl8HNRd8INmL4kUcMcZOPCbjk2ZNUdacsGeOOotkknHUeqV9L~1eJFztsSZ8kpq")
.UseContactPoint("6ce770b8-cd6d-4977-854c-bff92d0d3d98.zeebe.camunda.io:443")
.Build();
To deploy a process you can use the NewDeployCommand
method, which allows
to specify a list of resources, which should be deployed.
await zeebeClient.NewDeployCommand()
.AddResourceFile("send-email.bpmn")
.Send();
To start a new instance you can specify the bpmnProcessId
, i.e.
send-email
and optionally process variables.
var processInstanceResponse = await zeebeClient
.NewCreateProcessInstanceCommand()
.BpmnProcessId("send-email")
.LatestVersion()
.Variables(variables)
.Send();
To complete a service task,
a job worker has
to be subscribed to the task type defined in the process, i.e. email
. For this
the NewWorker
method can be used.
zeebeClient.NewWorker()
.JobType("email")
.Handler(JobHandler)
.MaxJobsActive(3)
.Timeout(TimeSpan.FromSeconds(10))
.PollInterval(TimeSpan.FromMinutes(1))
.PollingTimeout(TimeSpan.FromSeconds(30))
.Name("CsharpGetStartedWorker")
.Open()
The job worker will call for each activated job the specified job handler, which can look like this:
void JobHandler(IJobClient jobClient, IJob activatedJob)
{
var variables = JsonConvert.DeserializeObject<Dictionary<string, string>>(activatedJob.Variables);
Log.LogInformation($"Sending email with message content: {variables[VariablesKey]}");
jobClient.NewCompleteJobCommand(activatedJob).Send();
}
To make an job available for that job worker, a user task has to be completed, follow the instructions in the guide.