简体   繁体   中英

Call methods inside webapi synchronously

I have POST method which is saving data into database.

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

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. When Processing of one SaveDataIntoDB() method gets completed , it should call other. What can be done to do so?

Currently it is not doing so.

What you are looking for is a queue (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> . 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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