Package | Release | Pre-release |
---|---|---|
BeanstalkWorker.SimpleRouting |
CI | Status | Platform(s) | Framework(s) | Test Framework(s) |
---|---|---|---|---|
AppVeyor | Windows |
nestandard2.0 |
netcoreapp3.1 |
Allows to route a SQS
message to a specific endpoint on the Worker
instead of having a single endpoint handling all the messages.
Relies on the SQS message attributes. This is distributed via a NuGet
package but the implementation is so simple that you can just copy the classes into your own solution if that works better for you.
// Instantiate your model
StronglyTypedMessage model = new StronglyTypedMessage();
var sendMessageRequest = new SendMessageRequest
{
// Serialize your model as JSON (you can use Newtonsoft.Json if you prefer)
MessageBody = System.Text.Json.JsonSerializer.Serialize(model)
// Set the QueueUrl and other properties as you see fit
};
// AddRoutingAttribute is an extension method in the "BeanstalkWorker.SimpleRouting" namespace
sendMessageRequest.MessageAttributes.AddRoutingAttribute("task-name");
A sample Web
app is provided in samples/SampleWeb.
You can send two distinct types of messages by hitting two different endpoints:
GET http://localhost:5000/send/work
GET http://localhost:5000/send/nothing
Create a iAM
user (if you don't have one already) which has access to SQS
.
You'll need to configure four settings using user secrets:
Aws:RegionSystemName
- region code, for exampleap-southeast-2
Aws:Queue:WorkerQueueUrl
-URL
of theSQS
queue, for examplehttps://sqs.ap-southeast-2.amazonaws.com/375985941080/dev-gabriel
Aws:Queue:AccessKeyId
- this is theAccess key ID
of youriAM user
Aws:Queue:SecretAccessKey
- this is theSecret access key
of youriAM user
In the Configure
method of your Startup
class:
public void Configure(IApplicationBuilder app)
{
// The simple routing middleware needs to be added **before** configuring endpoint routing
app.UseHeaderRouting();
app.UseRouting();
app.UseEndpoints(endpoint => endpoint.MapControllers());
}
// This is important, we do not want a prefix in front of the action's route
[Route("")]
[ApiController]
public class SomeController : ControllerBase
{
// The route template has to match the argument given to AddRoutingAttribute
[HttpPost("task-name")]
public async Task<IActionResult> SomeMethod(StronglyTypedMessage model)
{
// Abbreviated for clarity
}
}
A sample Worker
app is provided in samples/SampleWorker.
If you wish to run the Worker
without deploying to AWS Beanstalk
you can leverage my Beanstalk Seeder project.
- Does not support periodic tasks
- It could be added fairly easily if required
netstandard2.0
and above only