[英]C# ASP.NET Core Web API: How to maintain context in the controller between calling different methods
C# ASP.NET Core Web API: general newbie question. C# ASP.NET 核心 Web API:一般新手问题。 Trying to implement basic Asynchronous Request-Reply Pattern.尝试实现基本的异步请求-回复模式。 Basic idea is that:基本思想是:
POST {url}/DataProcessor/DataX
with some payload客户端使用一些负载调用POST {url}/DataProcessor/DataX
GET {url}/DataProcessor/DataXStatus
, until HTTP Status Code of 200
is returned客户端使用GET {url}/DataProcessor/DataXStatus
检查交易状态,直到返回 HTTP 状态码200
xActionId
from the response above and then calls GET {url}/DataProcessor/GetResults/{xActionId}
to retrieve results of data processing.客户端从上面的响应中获取xActionId
,然后调用GET {url}/DataProcessor/GetResults/{xActionId}
来获取数据处理的结果。 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.这是我的 controller 的样子:当我调用DataXStatus
方法时(在正确调用DataX
方法之后),_processData 不在正确的_processData
中,就像它在DataX
方法完成后从 scope 中消失了。 What am I doing wrong?我究竟做错了什么? Is DataProcessorController
object gone after any one method is complete?任何一种方法完成后DataProcessorController
object 是否消失? 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()});
}
}
Answering this on my mobile so please forgive any typos.在我的手机上回答这个问题,所以请原谅任何错别字。
Basically, yes the class is re-instaintaited on every request.基本上,是的,class 会根据每个请求重新染色。 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: api 不会保留以前请求的上下文,除非您跟踪的 object 是 static,这是我的第一个解决方案:
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.这使您可以选择将“processData”class 作为 singleton 运行。这基本上意味着它是 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. web api 中有大量用于处理异步请求的库。 I think you should use hangfire for this, it takes a few minutes to set up and also has tons of configurion options.我认为你应该为此使用 hangfire,它需要几分钟的时间来设置并且还有大量的配置选项。
https://docs.hangfire.io/en/latest/getting-started/as.net-core-applications.html https://docs.hangfire.io/en/latest/getting-started/as.net-core-applications.html
Let me know if you need any further help!如果您需要任何进一步的帮助,请告诉我!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.