This project is not maintained anymore and is deprecated. The reason for this decision is the resent fork of this project by zeebe-client-csharp-accelerator. Please switch to the zeebe-client-csharp-accelerator which is a copy of this project with some small functional changes in the interface. Because of this switching should be relatively easy.
Apologies for the inconvenience but for the long term it will be better for the community to have one code base which is actively supported.
This project is an extension of the C# Zeebe client project. Zeebe Job handlers are automaticly recognized and bootstrapped via a .Net HostedService.
Read the Zeebe documentation for more information about the Zeebe project.
- net standard 2.0 or higher, which means
- .net core 2.1 or higher
- or .net framework 4.7.1 or higher
- latest C# Zeebe client release
- latest Zeebe release
The Zeebe C# client bootstrap extension is available via nuget (https://www.nuget.org/packages/zb-client-bootstrap/).
See examples and blog post for more information.
All classes which implement IJobHandler<TJob>
, IJobHandler<TJob, TResponse>
, IAsyncJobHandler<TJob>
or IAsyncJobHandler<TJob, TResponse>
are automaticly found, added to the service collection and autowired to Zeebe when you register this bootstrap project with the IServiceCollection.BootstrapZeebe()
extension method.
The BootstrapZeebe
method has two parameters:
ZeebeBootstrapOptions
via configuration, action delegate or both.- An array with assemblies which will be scanned for job handlers.
ConfigureServices((hostContext, services) => {
services.BootstrapZeebe(
hostContext.Configuration.GetSection("ZeebeBootstrap"),
this.GetType().Assembly
);
})
The job is an implementation of AbstractJob
. By default the simple name of the job is mapped to BPMN task job type. Job types must be unique. The default job configuration can be overwritten with AbstractJobAttribute
implementations, see attributes for more information.
public class SimpleJob : AbstractJob
{
public SimpleJob(IJob job)
: base(job)
{
//Variable mapping logic can be added here.
}
}
There is also a generic version AbstractJob<TState>
which will automaticly deserialize job variables into a typed object. Each property is automaticly added to the FetchVariables
collection when the FetchVariablesAttribute
is not used.
public class SimpleJob : AbstractJob<SimpleJobState>
{
public SimpleJob(IJob job, SimpleJobState state)
: base(job, state)
{ }
}
public class SimpleJobState
{
public bool Test { get; set; }
}
The job handler is an implementation of IJobHandler<TJob>
, IJobHandler<TJob, TResponse>
, IAsyncJobHandler<TJob>
or IAsyncJobHandler<TJob, TResponse>
. Job handlers are automaticly added to the DI container, therefore you can use dependency injection inside the job handlers. The default job handler configuration can be overwritten with AbstractJobHandlerAttribute
implementations, see attributes for more information.
public class SimpleJobHandler : IAsyncJobHandler<SimpleJob>
{
public async Task HandleJob(SimpleJob job, CancellationToken cancellationToken)
{
//TODO: make the handling idempotent.
await Usecase.ExecuteAsync(cancellationToken);
}
}
A handled job has three outcomes:
- The job has been handled without exceptions: this will automaticly result in a
JobCompletedCommand
beeing send to the broker. TheTResponse
is automaticly serialized and added to theJobCompletedCommand
. - An exception has been thrown while handling the job and the exception implements
AbstractJobException
: this wil automaticly result in aThrowErrorCommand
beeing send to the broker; - Any other exception will automaticly result in a
FailCommand
beeing send to the broker;
The JobCompletedCommand
accepts variables which are added to process instance. For this use case the job handler can be use with a second generic parameter IJobHandler<TJob, TResponse>
. The response is automaticly serialized.
public class SimpleJobHandler : IAsyncJobHandler<SimpleJob, SimpleResponse>
{
public async Task<SimpleResponse> HandleJob(SimpleJob job, CancellationToken cancellationToken)
{
//TODO: make the handling idempotent.
var result = await Usecase.ExecuteAsync(cancellationToken);
return new SimpleResponse(result);
}
}
IPublishMessageCommandStep3
,ICreateProcessInstanceCommandStep3
andISetVariablesCommandStep1
are extended with theState(object state)
method which uses the registeredIZeebeVariablesSerializer
service to automaticly serialize state and pass the result to theVariables(string variables)
method.
This project uses the following conventions:
- By default the simple name of the
AbstractJob
implementation is used to match theType
which is specified in the BPMN model. This can be overriden by adding theJobTypeAttribute
to theAbstractJob
implementation, see attributes for more information. - By default the assembly name which contains the job handler is used as the
Worker name
. This can be overriden by adding theWorkerNameAttribute
to theAbstractJob
implementation, see attributes for more information. - By default the job handlers are added to de DI container with a
Transient
service lifetime. This can be overriden by adding theServiceLifetimeAttribute
to the job handler, see attributes for more information. - By default the
ZeebeVariablesSerializer
is registered as the implementation forIZeebeVariablesSerializer
which usesSystem.Text.Json.JsonSerializer
. You can override this registration by registering your service after theBootstrapZeebe
method or you can registerSystem.Text.Json.JsonSerializerOptions
to configure theSystem.Text.Json.JsonSerializer
.
Run dotnet build Zeebe.Client.Bootstrap.sln
Run dotnet test Zeebe.Client.Bootstrap.sln