简体   繁体   English

同步调用webapi中的方法

[英]Call methods inside webapi synchronously

I have POST method which is saving data into database. 我有将数据保存到数据库中的POST方法。

[POST]
public void SaveData()
{
  try
  {
    BusinessLayer businessLayerObject = new BusinessLayer();
    businessLayerObject.SaveDataIntoDB();
  }
  catch(Exception ex)
  {}
}

BusinessLayer.cs :- BusinessLayer.cs:-

public void SaveDataIntoDB()
{
  try
  {
    using (var context = new EntityContext())
   {
    using (DbContextTransaction transaction=context.Database.BeginTransaction())
    {
 .... ......
    }
   }
  }
catch(Exception ex)
  {}
}

When multiple requests are there at a same time for SaveData method, I want SaveDataIntoDB() method to get called synchronously. 当同时有多个请求SaveData方法时,我希望SaveDataIntoDB()方法被同步调用。 When Processing of one SaveDataIntoDB() method gets completed , it should call other. 当一个SaveDataIntoDB()方法的处理完成时,它应调用其他方法。 What can be done to do so? 可以做什么呢?

Currently it is not doing so. 当前它没有这样做。

What you are looking for is a queue (FIFO). 您正在寻找的是队列(FIFO)。 How to implement that depends highly on how your deployment is. 如何实施在很大程度上取决于您的部署方式。

If you're using a single instance deployment, you can create a class that wraps a Queue<T> . 如果使用的是单实例部署,则可以创建一个包装Queue<T> It should be implemented as a singleton and in a async fashion sends the queued messages to the database. 它应实现为单例,并以异步方式将排队的消息发送到数据库。

If you're using a scale out approach, you will need something like RabbitMQ, MSMQ or other message queue... 如果您使用横向扩展方法,则需要RabbitMQ,MSMQ或其他消息队列。

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

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