0
我有一個API,其中我做了很多處理,比如發送服務總線隊列消息並接收它,向表中添加條目,然後將事件發送到socket.io服務器。我希望所有這些都受到併發執行的保護。我正在使用Lock,但似乎沒有辦法。我錯過了什麼?以下是我的代碼使用鎖定停止併發執行不起作用?
public class BroadcastController : ApiController
{
private readonly Object _Lock = new Object();
[HttpPost]
[ActionName("UploadRecording")]
public async Task<HttpResponseMessage> UploadRecording()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string path = Path.GetTempPath();
var provider = new MultipartFormDataStreamProvider(path);
// Read the form data and return an async task.
var response = await Request.Content.ReadAsMultipartAsync(provider);
// processing the mime content
lock (_Lock)
{
// sending and receiving service bus messages
// adding records to table
// sending an event to socket.io
return Request.CreateResponse(HttpStatusCode.OK, new ResponseMessage<PersistedAudioRecord> { SuccessCode = 1, Message = "Broadcast Uploaded", Data = updatedRecord });
} } }
你在'BroadcastController'的_same instance_上做了多個'UploadRecording'調用嗎?或者你是否在創建這個類的多個實例?您現在擁有的鎖只會用於_per instance_,並且不會在多個實例中共享。 (如果你確實有多個實例,只需將'_Lock'重新定義爲'static') –
Damnn !!傻我!非常感謝:D – Bitsian