问题描述
我有将数据保存到数据库中的POST方法.
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:-
BusinessLayer.cs :-
public void SaveDataIntoDB()
{
try
{
using (var context = new EntityContext())
{
using (DbContextTransaction transaction=context.Database.BeginTransaction())
{
.... ......
}
}
}
catch(Exception ex)
{}
}
当同时有多个请求SaveData方法时,我希望SaveDataIntoDB()方法被同步调用.当完成一个SaveDataIntoDB()方法的处理时,应调用其他方法.可以怎么做呢?
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?
当前没有这样做.
推荐答案
您正在寻找的是队列(FIFO).实施方法在很大程度上取决于您的部署方式.
What you are looking for is a queue (FIFO). How to implement that depends highly on how your deployment is.
如果使用的是单实例部署,则可以创建一个包装 Queue< T>
的类.它应实现为单例,并以异步方式将排队的消息发送到数据库.
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.
如果使用横向扩展方法,则需要RabbitMQ,MSMQ或其他消息队列之类的东西...
If you're using a scale out approach, you will need something like RabbitMQ, MSMQ or other message queue...
这篇关于同步调用webapi中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!