I have ASP.Net Core 2.1
application with AWS
as Cloud. And want to implement a Queue mechanism.
I have an API as below.
public class UserControler : ControllerBase
{
[HttpPost]
public asyn Task<IActionResult> Post([FromBody]UserTO user)
{
//Step#1 Insert the user record in the db
bool flag = await _userService.Add(user).ConfigureAwait(false); // async Task
//Step#2 If the db operation is successful, send an email to user email
if(flag)
await _emailService.Send(user.email).ConfigureAwait(false); //async Task
}
}
The HttpResponse
is returned after the two async methods are executed. But what I want is
It's just once case, the queue task could be anything like sendemail or uploadtos3, createsomelog anything etc.
I have no clue on how can I do this, hence please bear with me for this .
Thanks!
It seems like a queue would be overkill for this instance, but you could definitely use Simple Queue Service for this.
When you insert an item to SQS, you can set it up to have a Lambda function be triggered and pull the item off the queue and process it. Here is a good tutorial from AWS on how this can be done. The .NET SDK also has some decent documentation and examples on using SQS.
But, if it was me, I would probably just opt for using a Dynamo Stream to trigger a lambda function. Basically, once the row is inserted, you can trigger a lambda function to execute and process some data. So, in your case, upon insert, you can trigger a function to send an email to that user using data from that row itself.
This is an example of the Lambda code that would be executed.
using System;
using System.IO;
using System.Threading.Tasks;
using Amazon.DynamoDBv2.Model;
using Amazon.Lambda.Core;
using Amazon.Lambda.DynamoDBEvents;
using Newtonsoft.Json;
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace LocalDynamoDbStream
{
public class Function
{
private static readonly JsonSerializer JsonSerializer = new JsonSerializer();
public Task FunctionHandler(DynamoDBEvent dynamoEvent, ILambdaContext context)
{
context.Logger.LogLine($"Beginning to process {dynamoEvent.Records.Count} records...");
foreach (var record in dynamoEvent.Records)
{
context.Logger.LogLine($"Event ID: {record.EventID}");
context.Logger.LogLine($"Event Name: {record.EventName}");
var streamRecordJson = SerializeStreamRecord(record.Dynamodb);
context.Logger.LogLine("DynamoDB Record:");
context.Logger.LogLine(streamRecordJson);
// Do your email here using data from new inserted row
var email = record.Dynamodb.NewImage["email"].S;
await _emailService.Send(email).ConfigureAwait(false);
}
context.Logger.LogLine("Stream processing complete.");
return Task.CompletedTask;
}
private static string SerializeStreamRecord(StreamRecord streamRecord)
{
using (var writer = new StringWriter())
{
JsonSerializer.Serialize(writer, streamRecord);
return writer.ToString();
}
}
}
}
See more here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.