The C# .NET SDK currently only supports legacy versions of Nitric prior to v1. This version is maintained for compatibility with existing projects and not recommended for new projects. New projects should be started using a supported SDK (presented automatically using the `nitric new` command) orget in touch to request an update to the latest version.
.NET - Queue.Send()
Send tasks to a queue.
using Nitric.Sdk;
using Nitric.Sdk.Queue;
class UserRequest
{
public string UserId { get; set; }
public string Message { get; set; }
}
var batchQueue = Nitric.Queue<UserRequest>("batch").With(QueuePermission.Sending);
batchQueue.Send(new UserRequest
{
UserId = "1234",
Message = "Hello!"
});
Nitric.Run();
Parameters
- Name
tasks
- Required
- Required
- Type
- Task<T> | Task<T>[] | T | T[]
- Description
The task or an array of tasks to send to the queue.
- Name
Id
- Optional
- Optional
- Type
- string
- Description
unique id to apply to the task.
- Name
Payload
- Required
- Required
- Type
- T
- Description
payload to send with the task.
- Name
PayloadType
- Optional
- Optional
- Type
- string
- Description
a hint to the type of payload supplied.
Examples
Send a task to a queue
using Nitric.Sdk;
using Nitric.Sdk.Queue;
using System.Collections.Generic;
class UserRequest
{
public string UserId { get; set; }
public string Message { get; set; }
}
var batchQueue = Nitric.Queue<UserRequest>("batch").With(QueuePermission.Sending);
batchQueue.Send(
new Task<UserRequest>
{
Id = "1234",
PayloadType = "user_request",
Payload = new UserRequest
{
UserId = "user_1234",
Message = "Hello!"
}
}
);
Nitric.Run();
Send multiple tasks to a queue
using Nitric.Sdk;
using Nitric.Sdk.Queue;
using System.Collections.Generic;
var batchQueue = Nitric.Queue("batch").With(QueuePermission.Sending);
batchQueue.Send(
new Task[]
{
new UserRequest
{
UserId = "user_1",
Message = "Batch Task 1"
}, new UserRequest
{
UserId = "user_2",
Message = "Batch Task 2"
}
}
);
Nitric.Run();
Dealing with failures
In rare cases when sending tasks to a queue some tasks might fail to be sent. The response from send()
will include an array of any tasks that failed to send. You can process this array to retry or log the error.
var failed = await batchQueue.Send(tasks);
failed.ForEach(failedTask => {
Console.WriteLine(failedTask.ID);
});
Nitric.Run();