C# 如何在asp.net web api中对请求进行排队并在服务器端执行?

C# 如何在asp.net web api中对请求进行排队并在服务器端执行?,c#,multithreading,asp.net-web-api2,C#,Multithreading,Asp.net Web Api2,我在服务器端有使用共享数据的方法,我需要在不同请求之间同步数据访问。例如 BrowserClient1_Calling_Method1() { //This should be an XHR call and it waits for the result. } BrowserClient2_Calling_Method2() { //This should be an XHR call and it waits for the result. } 在服务器端,我有两个方

我在服务器端有使用共享数据的方法,我需要在不同请求之间同步数据访问。例如

BrowserClient1_Calling_Method1()
{
    //This should be an XHR call and it waits for the result. 
}

BrowserClient2_Calling_Method2()
{
    //This should be an XHR call and it waits for the result. 
}
在服务器端,我有两个方法需要逐个调用

//When ever this method is called, put it in some sort of queue so that 
//Method1 and Method2 gets executed one by one.  
Server_Method1()
{
    //Access the SHARED data
}


//When ever this method is called, put it in some sort of queue so that 
//Method1 and Method2 gets executed one by one.
Server_Method2()
{
    //Access the SHARED data
}
因此,我的问题是,我如何使用一些
ConcurrentQueue
软件,一个接一个地执行服务器方法,并
使客户端不知道这样的队列。
从客户端角度看,它应该像任何其他XHR请求一样

我尝试在c#中使用
BlockingCollection
,但不确定应该遵循哪种模式


请注意,我可以在服务器方法中使用“lock”,但我不想使用

当您使用共享变量时,最安全、最好的方法是使用锁模式(有多种选项可以处理此问题)

对于问题的第二部分,最好的方法是使用异步方法,这样您的客户机才知道您的队列,在执行他们的请求后,您可以向他们发送信号以显示相关响应(使用signal-r)

如果你需要更多的细节,请告诉我描述你每个部分