Setup application
In this section, you'll learn how to set up an application that can interact with the Taskurai API.
Prerequisites
- You completed the Getting started tutorial.
- You created a personal access token
Authentication
Personal access tokens (PAT) are used to secure access to the Taskurai API.
There are two different scenarios for using the Taskurai API:
- Local development
- Hosted environment (your dev, test, production, etc. environment)
The lifetime of the access token should be matched to the use-case:
- Short for local development
- Can have an unlimited lifetime if needed for hosted environments.
Read Worker Setup for more information about authentication in worker applications.
It is strongly advised to handle the access token with care. In .NET, we suggest using user-secrets when developing locally. In your hosted environment, use the appropriate solution to handle sensitive data.
Setting up an Asp.Net Core application
Application Setup
In this sample, you will learn how to create an Asp.Net Core WebApi sample application.
- Create a sample directory:
- macOS
- Windows
- Linux
mkdir tasks-aspnetcore-sample
cd tasks-aspnetcore-sample
md tasks-aspnetcore-sample
cd tasks-aspnetcore-sample
mkdir tasks-aspnetcore-sample
cd tasks-aspnetcore-sample
- Create a new web API application:
- C#
.NET 8.0 SDK is recommended for this example.
dotnet new webapi --framework net8.0
- Add Taskurai SDK:
dotnet add package Taskurai
dotnet add package Taskurai.AspNetCore
- Setup the Taskurai client:
Change the Program.cs
file:
...
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
...
builder.Services.AddTaskurai(); // Add Taskurai
...
var app = builder.Build();
...
- Add application configuration:
Add the following configuration files:
{
"Taskurai": {
"IsolationMode": false
}
}
{
"Taskurai": {
"IsolationMode": true
}
}
- Add the personal access token as a user secret, replace the PAT with the one created in the previous step:
dotnet user-secrets init
dotnet user-secrets set "Taskurai:Pat" "e8gFaA8ZaetAHiS257opAr..."
- Next, run the application to validate the setup:
dotnet run
The expected output should be:
Building...
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5017
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: /Users/taskuraidev/Dev/taskurai/samples/tasks-aspnetcore-sample
The application runs in Isolation Mode when the environment is set to Development mode.
In Isolation Mode, the tasks created and returned are isolated to your local session and will not affect workers running in the Taskurai instance.
Using Taskurai in controllers
To use Taskurai in an Asp.Net Core Controller, you can inject the TaskuraiClientFactory:
- C#
...
public class OrderController : ControllerBase
{
private readonly ILogger<OrderController> _logger;
private readonly TaskuraiClient _taskurai;
public OrderController(ILogger<OrderController> logger, ITaskuraiClientFactory taskuraiClientFactory)
{
_logger = logger;
_taskurai = taskuraiClientFactory.Create(); // Create a Taskurai client
}
...
public async Task<OrderResponse> CreateOrder()
{
...
var orderResponse = new OrderResponse()
{
OrderId = Guid.NewGuid(),
CustomerName = customerName,
OrderTimestamp = DateTime.UtcNow
};
// Generate an order confirmation (pdf + email)
var taskInput = new TaskConfig("generateOrderConfirmation")
{
Arguments = new List<TaskArgument>()
{
new TaskArgument("orderId")
{
Value = orderResponse.OrderId.ToString()
}
},
Category = "Create order",
Subject = $"Creating order {orderResponse.OrderId.ToString()} for customer {customerName}"
};
var taskResponse = await _taskurai.CreateTaskAsync(taskInput);
// Store the task id for the client to follow up
orderResponse.OrderConfirmationTaskId = taskResponse.Id;
return orderResponse;
}
...
}
Setting up a console application
Follow the Getting started tutorial to learn how to set up a solution from scratch.
- Create a sample directory:
- macOS
- Windows
- Linux
mkdir tasks-console-sample
cd tasks-console-sample
md tasks-console-sample
cd tasks-console-sample
mkdir tasks-console-sample
cd tasks-console-sample
- Create a new sample console application:
- C#
.NET 8.0 SDK is recommended for this example.
dotnet new console --framework net8.0
- Add Taskurai SDK:
dotnet add package Taskurai
- Add configuration libraries:
dotnet add package Microsoft.Extensions.Configuration
dotnet add package Microsoft.Extensions.Configuration.Json
dotnet add package Microsoft.Extensions.Configuration.Binder
dotnet add package Microsoft.Extensions.Configuration.UserSecrets
- Setup the Taskurai client:
Add the following code to your Program.cs
file:
using Taskurai;
using Taskurai.Models;
using System.Text.Json;
using Microsoft.Extensions.Configuration;
using System.Text.Json.Serialization;
Console.WriteLine("Testing Taskurai client library...");
// Load configuration - defaults to Development
var environment = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Development";
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true)
.AddUserSecrets<Program>()
.Build();
// Setup Taskurai client
var taskurai = new TaskuraiClient(configuration["Taskurai:Pat"], new TaskuraiClientOptions()
{
IsolationMode = configuration.GetValue<bool>("Taskurai:IsolationMode", true)
});
- Add application configuration:
Add the following configuration files:
{
"Taskurai": {
"IsolationMode": false
}
}
{
"Taskurai": {
"IsolationMode": true
}
}
- Add the personal access token as a user secret, replacing the PAT with the one created in the previous step:
dotnet user-secrets init
dotnet user-secrets set "Taskurai:Pat" "e8gFaA8ZaetAHiS257opAr..."
- Next, run the console application to validate the setup:
dotnet run
The expected output should be:
Testing Taskurai client library...
Entering isolation mode, looking up token information...
Running in isolation mode.
The console application runs in Isolation Mode when the environment is set to Development mode.
In Isolation Mode, the tasks created and returned are isolated to your local session and will not affect workers running in the Taskurai instance.