How to call HttpPost method from other namespace?

Viewed 32

I am working on a project with AWS services (SQS and DynamoDB). It is my first project using them so it is a little test project to see how everything works. I almost have what I want to achieve which is posting the message gained from the SQS queue and putting it inside a dynamoDB table.

I can put stuff inside a dynamoDB table and I can fetch from the SQS queue, but now I am stuck on simply calling the method from the controller and passing the message body.

Controller class:

using Microsoft.AspNetCore.Mvc;
using Amazon.DynamoDBv2.DataModel;
using TimelineService.Model;

namespace TimelineService.Controller
{
    [ApiController]
    [Route("api/[controller]")]
    public class TimelineController : ControllerBase
    {
        private readonly IDynamoDBContext _context;

        public TimelineController(IDynamoDBContext context)
        {
            _context = context;
        }

        [HttpPost]
        public async Task<IActionResult> CreatePost(Post postRequest)
        {
            var post = await _context.LoadAsync<Post>(postRequest.Id);
            if (post != null) return BadRequest($"Post with Id {postRequest.Id} Already Exists");
            await _context.SaveAsync(postRequest);
            return Ok(postRequest);
        }

    }
}

The SQS Processor class

using Amazon.Runtime;
using Amazon.SQS;
using Amazon;
using Amazon.SQS.Model;

namespace TimelineService.SQSProcessor
{
    public class TimelineProcessor : BackgroundService
    {
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            Console.WriteLine("Starting background processor");
            var credentials = new BasicAWSCredentials("MYCREDENTIALS", "MYCREDENTIALS");
            var client = new AmazonSQSClient(credentials, RegionEndpoint.EUCentral1);

            while (!stoppingToken.IsCancellationRequested)
            {
                Console.WriteLine($"Getting messages from the queue {DateTime.Now}");
                var request = new ReceiveMessageRequest()
                {
                    QueueUrl = "https://sqs.eu-central-1.amazonaws.com/075206908135/PostTimelineQueue",
                    WaitTimeSeconds = 15,
                    VisibilityTimeout = 20//for long polling

                };
                var response = await client.ReceiveMessageAsync(request);
                foreach (var message in response.Messages)
                {
                    Console.WriteLine(message.Body);
                    if (message.Body.Contains("Exception")) continue; //send to dead letter queue if message contains exception
                    //call createmethod and put message body inside 

                    await client.DeleteMessageAsync("MySQS", message.ReceiptHandle);
                }
            }
        }
    }
}

In the TimelineProcessor class I have commented the following line

 //call createmethod and put message body inside

where I want the posting into the dynamoDB to happen. I simply need to call the CreatePost method from the controller class and pass the message body with it, but I can seem to get it to work.

Since working with c# and ASP.net was a long time ago I just forgot how I should do it and can't seem to google it.

Can anyone help me? Thanks in advance!

0 Answers
Related