Allow only one call to web service method at a time

Nazza 0 Reputation points
2024-10-02T12:35:22.6366667+00:00

Hi all,

I have an ASP.NET web service installed on IIS. I need to permit only one concurrent call to a specific web method? How can I achieve this?

Can I use mutex? I don't have such in-depth knowledge of application pool, processes and threads...

Thank you very much!

Internet Information Services
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,504 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 66,226 Reputation points
    2024-10-02T16:59:25.9033333+00:00

    if its not a webfarm (more than one webserver server) and you just want to serialize the request, you can use a lock or mutex.

      private static object _myLock = new object();
      public async Task<MyRs> Sample(MyRq request)
      {
          lock(_myLock)
          {
             ....
             return rs;
          }
      }
    
    

    note: if the code takes too long, you have timeout if requests are stacked too much. (be sure its a static variable)

    0 comments No comments

  2. SurferOnWww 3,031 Reputation points
    2024-10-03T00:49:50.75+00:00

    I need to permit only one concurrent call to a specific web method?

    Can you let us know why? If you are considering something like the concurrency control in database access, there should be better way such as:

    Implementing Optimistic Concurrency (C#)

    0 comments No comments

  3. XuDong Peng-MSFT 10,741 Reputation points Microsoft Vendor
    2024-10-03T06:31:25.9266667+00:00

    Hi @Nazza,

    I don't have such in-depth knowledge of application pool, processes and threads...

    The Application Pool contains one Worker Process by default. You can configure it to multiple Worker Processes to enable Web Garden. Each Worker Process can manage multiple threads.

    You can assign multiple sites to the same application pool, and they will all run under the same process, but they will run under different "app domains" which separate the code for the different sites.

    Can I use mutex?

    As mentioned by other community members, if your environment is a single server, just use lock or mutex can implement your needs, otherwise you may need distributed locking to make it work.

    Best Regards,

    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  4. Nazza 0 Reputation points
    2024-10-04T09:15:16.8666667+00:00

    Thank you all!

    this is the right way but I just discovered I cannot use Lock/Mutex mechanisms because I have to call 2 sequential asynchronous methods in the interested piece of code (to call SAP Restful services).

    Thank you again!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.