0

C# ASP.NET Core Web API: general newbie question. Trying to implement basic Asynchronous Request-Reply Pattern. Basic idea is that:

  1. Client calls POST {url}/DataProcessor/DataX with some payload
  2. Client checks on the status of the transaction using GET {url}/DataProcessor/DataXStatus, until HTTP Status Code of 200 is returned
  3. Client gets xActionId from the response above and then calls GET {url}/DataProcessor/GetResults/{xActionId} to retrieve results of data processing.

This is what my controller looks like: When I call DataXStatus method (after properly calling the DataX method), the _processData isn't in the right state, like it has gone out of scope after DataX method is done. What am I doing wrong? Is DataProcessorController object gone after any one method is complete? What is the right way to do this?

Thanks in advance

[Route("[controller]")]
[ApiController]
public class DataProcessorController : ControllerBase
{
    private ProcessData _processData = new ProcessData() ;

    [HttpPost]
    [Route("DataX")]
    public IActionResult DataX([FromBody] xData value)
    {
        _processData.CalculateResult(value);
        return Accepted();
    }

    [HttpGet]
    [Route("DataXStatus")]
    public IActionResult DataXStatus()
    {
        if(_processData.IsRunning())
        {
            return Accepted();
        }
        return Ok(new xDataStatus(){id = _processData.GetxActionId()});
    }

    [HttpGet]
    [Route("GetResults/{xActionId}")]
    public IActionResult GetResults(string xActionId)
    {
        return Ok(new xDataResults(){resX = _processData.GetResults()});
    }
}
3
  • 1
    I believe you'll get a newly constructed class object each time a request is made, so your constructor is creating new ProcessData() for Post and then for Get. Commented May 3, 2022 at 21:30
  • hmm.. so what is the proper way to maintain ProcessData object so that all methods in the DataProcessorController method able to access it? is it even valid question? Commented May 3, 2022 at 21:50
  • not sure that you need to... (do it all in post... ) but you if you do, you might use a database or some kind of client side storage. Maybe include in your post what the overall task is. Commented May 3, 2022 at 21:59

1 Answer 1

1

Answering this on my mobile so please forgive any typos.

Basically, yes the class is re-instaintaited on every request. The api does not keep context of previous requests unless the object you are keeping track of is static, which being me to my first solution for this:

Quick and dirty option:

I would recommend you use dependency injection for this instead. That gives you the option to run your 'proccessData' class as a singleton. Which basically means it's static.

Better option:

The other more correct way is to use a known library for this. There are are tons of libraries that are built for handling async requests in web apis. I think you should use hangfire for this, it takes a few minutes to set up and also has tons of configurion options.

https://docs.hangfire.io/en/latest/getting-started/aspnet-core-applications.html

Let me know if you need any further help!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.