Skip to main content

Working with tasks

In this step, we will extend the console application created in the previous step.

Creating a new task

Add the following code to your sample application to create your first task:

var serializerOptions = new JsonSerializerOptions()
{
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
Converters = {
new JsonStringEnumConverter()
}
};

// Custom task payload
{
ExternalId = "my-id-123456",
Arguments = new List<TaskArgument>()
{
new TaskArgument()
{
Name = "id",
Value = "abc-123"
},
new TaskArgument()
{
Name = "data",
Data = BinaryData.FromObjectAsJson(new
{
Name = "test",
Value = "Some value",
some = new List<string>()
{
"Property1",
"Property2"
}
}),
Sensitive = true
}
},
Category = "Console app",
Subject = "Samples",
Description = "This is a test task"
};


var createdTask = taskurai.CreateTask(createTaskInput);

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

Next, run the console application to validate:

dotnet run

The expected output should be:

{
"Id": "5e7f3b48-8914-478e-8b6c-35c8d5924c7a",
"Config": {
"Command": "testCommand",
"Arguments": [
{
"Name": "arg1",
"Value": "abc-123"
}
],
"Category": "Console app",
"Subject": "Samples",
"Description": "This is a test task",
"ExternalId": "my-id-123456",
"IsolationMode": true,
"LocalIdentifier": "6d63b7cf-a0f9-4448-ac8a-5a6edd8bdf7a",
"Id": "5e7f3b48-8914-478e-8b6c-35c8d5924c7a",
"Data": {"Name":"Test","Value":"Some value","Properties":["Some property","Another property"]}
},
"Created": "2023-06-15T17:51:25.7657589+00:00",
"Status": "Created",
"ProvisioningState": "Created",
"Archived": false,
"Postponed": false,
"Cancelled": false
}

Retrieving a task

Next, we retrieve the previously created task:

var singleTask = taskurai.GetTask(createdTask.Id); // Get the created task

Console.WriteLine(JsonSerializer.Serialize(singleTask));

Next, run the console application to validate:

dotnet run

The expected output should be:

{
"Id": "5e7f3b48-8914-478e-8b6c-35c8d5924c7a",
"Config": {
"Command": "testCommand",
"Arguments": [
{
"Name": "arg1",
"Value": "abc-123"
}
],
"Category": "Console app",
"Subject": "Samples",
"Description": "This is a test task",
"ExternalId": "my-id-123456",
"IsolationMode": true,
"LocalIdentifier": "6d63b7cf-a0f9-4448-ac8a-5a6edd8bdf7a",
"Id": "5e7f3b48-8914-478e-8b6c-35c8d5924c7a",
"Data": {"Name":"Test","Value":"Some value","Properties":["Some property","Another property"]}
},
"Created": "2023-06-15T17:51:25.7657589+00:00",
"Status": "Created",
"ProvisioningState": "Created",
"Archived": false,
"Postponed": false,
"Cancelled": false
}

Listing tasks

The following example lists the previously created tasks:

var tasks = taskurai.ListTasks(new ListTasksOptions() { Limit = 10} );

Console.WriteLine(JsonSerializer.Serialize(tasks, serializerOptions));

Next, run the console application to validate:

dotnet run

The expected output should be:

[
{
"Id": "5e7f3b48-8914-478e-8b6c-35c8d5924c7a",
"Config": {
"Command": "testCommand",
"Arguments": [
{
"Name": "arg1",
"Value": "abc-123"
}
],
"Category": "Console app",
"Subject": "Samples",
"Description": "This is a test task",
"ExternalId": "my-id-123456",
"IsolationMode": true,
"LocalIdentifier": "6d63b7cf-a0f9-4448-ac8a-5a6edd8bdf7a",
"Id": "5e7f3b48-8914-478e-8b6c-35c8d5924c7a",
"Data": {"Name":"Test","Value":"Some value","Properties":["Some property","Another property"]}
},
"Created": "2023-06-15T17:51:25.7657589+00:00",
"Status": "Created",
"ProvisioningState": "Created",
"Archived": false,
"Postponed": false,
"Cancelled": false
}
]

Deleting a task

The following example deletes the previously created task:

taskurai.DeleteTask(createdTask.Id);

Next, run the console application to validate:

dotnet run