Skip to main content

.NET SDK Information

The Taskurai SDK is designed to make it easy to use Taskurai services from your applications. The SDK contains all the functionality to create tasks, follow up on tasks, and manage tasks.

This section contains some important topics about using the SDK correctly.

Thread Safety

Client Objects

All SDK client objects are thread-safe and independent of each other. This design ensures that reusing client instances is always safe, even across threads. The following example creates multiple tasks in different threads and is thread-safe:

foreach (var taskInput in taskInputList)
{
// Using clients from parallel threads
Task.Run(() =>
{
var createdTask = taskurai.CreateTask(createTaskInput);

Console.WriteLine(JsonSerializer.Serialize(createdTask, serializerOptions));
});
}

If your project is an Asp.Net Core application, you can use the TaskuraiClientFactory to create a Taskurai client. The factory's Create method can be called multiple times, resulting in a singleton Taskurai client.

Model Objects

Model objects used by SDK clients, whether input or output models, aren't thread-safe by default. Most use cases involving model objects only use a single thread. The following code illustrates a bug in which accessing a model from multiple threads could cause undefined behavior:

var createdTask = taskurai.CreateTask(createTaskInput);

foreach (var order in orders)
{
// Don't use model type from parallel threads
Task.Run(() => createdTask.Result.Data.Orders.Add(order));
)

To access the model from different threads, you must implement your own synchronization code. For example:

var createdTask = taskurai.CreateTask(createTaskInput);

foreach (var tag in tags)
{
// Don't use model type from parallel threads
Task.Run(() =>
{
lock (createdTask)
{
createdTask.Result.Data.Orders.Add(order);
}
});
)

Advanced Configuration

The Taskurai SDK builds upon the same foundations as the Azure .NET SDK. The SDK has built-in retry policies to counter network or service failures and provide a more robust experience.

The advanced settings can be set when constructing a new Taskurai client using the TaskuraiClientOptions:

// Setup Taskurai client
var options = new TaskuraiClientOptions()
{
IsolationMode = false
};
options.Retry.MaxRetries = 3; // Configure the maximum number of retries

var taskurai = new TaskuraiClient(configuration["Taskurai:Pat"], options);

Or when using the client factory:

public class OrderController : ControllerBase
{
private readonly ILogger<OrderController> _logger;
private readonly TaskuraiClient _taskurai;

public OrderController(ILogger<OrderController> logger, ITaskuraiClientFactory taskuraiClientFactory)
{
_logger = logger;

// Setup Taskurai client
var options = new TaskuraiClientOptions()
{
};
options.Retry.MaxRetries = 3; // Configure the maximum number of retries
_taskurai = taskuraiClientFactory.Create(options);
}
}