简体   繁体   English

C# ASP.NET Core Web API:如何在controller调用不同方法之间保持上下文

[英]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:基本思想是:

  1. Client calls POST {url}/DataProcessor/DataX with some payload客户端使用一些负载调用POST {url}/DataProcessor/DataX
  2. Client checks on the status of the transaction using GET {url}/DataProcessor/DataXStatus , until HTTP Status Code of 200 is returned客户端使用GET {url}/DataProcessor/DataXStatus检查交易状态,直到返回 HTTP 状态码200
  3. Client gets 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.

相关问题 如何从控制器进行 HTTP 调用? 使用 Web API 的 Asp.Net Core C# - How to make HTTP call from Controller ? to Use web API's Asp.Net Core C# 如何在ASP.NET C#Web应用程序中维护库存? - How to maintain stock in asp.net C# web application? 在 ASP.NET Core Web API 控制器中使用 C# 7 元组 - Using a C# 7 tuple in an ASP.NET Core Web API Controller Why different behavior between ASP.NET Web API 2 & ASP.NET Core 3.1 Web API? - Why different behavior between ASP.NET Web API 2 & ASP.NET Core 3.1 Web API? 如何使用类似但不同的参数创建ASP.NET Core Web API控制器方法 - How to create ASP.NET Core Web API controller method with similar but different parameter C# ASP.NET Core Web API 包含与 where - C# ASP.NET Core Web API include with where 身份在 C# ASP.NET 核心 Web API 用户 - Identity in C# ASP.NET Core Web API user 使用 C# ASP.NET Core Web API 进行谷歌身份验证 - Google authentication with C# ASP.NET Core Web API Controller方法返回ActionResult的原因是什么? (ASP.NET Core Web API) - What's the reason why Controller methods return ActionResult? (ASP.NET Core Web API) 使用 C# HttpClient,如何将字符串 HttpContent POST 到 ASP.net Core Web API? - Using C# HttpClient, how to POST a string HttpContent to ASP.net Core web API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM