简体   繁体   English

Acknowledge the web api call before processing in Web Api C#

[英]Acknowledge the web api call before processing in Web Api C#

I have a main web app which sends the alert to second application and the second application here needs to do more work, meanwhile main application will be waiting for the acknowledgment, hence once the second app receives the request, it should acknowledge by sending a flag and then continue its work.我有一个主要的 web 应用程序,它将警报发送到第二个应用程序,这里的第二个应用程序需要做更多的工作,同时主应用程序将等待确认,因此一旦第二个应用程序收到请求,它应该通过发送标志来确认然后继续其工作。

In order to achieve that i am creating a new method and processing the data there and i am calling that new method using new thread and immediately returning the acknowledgement to main app.为了实现这一点,我正在创建一个新方法并在那里处理数据,我正在使用新线程调用该新方法并立即将确认返回给主应用程序。

Below is my code, am i following the right approach or is there a better way of doing it?下面是我的代码,我是在遵循正确的方法还是有更好的方法?

    public bool Post([FromBody] string content)
    {            
        if (!string.IsNullOrEmpty(content))
        {
            Thread thread = new Thread(() => AnotherMethod(content));
            thread.Start();
        }
        return true;
    }

You could try using async / await您可以尝试使用async / await

public async Task<bool> Post([FromBody] string content)
{            
    if (!string.IsNullOrEmpty(content))
    {
        await AnotherMethod(content);
    }
    return true;
}

then your AnotherMethod must be async too那么你的AnotherMethod必须是async

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM