skip to Main Content

Is it a good approach to make API method async?

I have an API which creates user and send a verification code to user email.
JWT token is generated after email is sent on user email so I have made the method async and also the API. So, is it a good approach or please guide what should be the best approach to handle such type of request.

I am using entity frame work so the problem is, sendEmail is a different method. If I don’t make is async it gives "Cannot access a disposed context instance".

2

Answers


  1. When you need to wait for the full response of a request, then you need the request to be synchronous. On the other hand, when you do not need to wait for the full request, then you can make it asynchronous.

    Login or Signup to reply.
  2. The use of asynchronous code can largely depend on your application and specific requirements.

    Running code asynchronously can add overhead due to thread switching, but this approach can also expedite your request-response cycle and enable your application to handle more requests concurrently.

    A common misconception is that using the async keyword will invariably result in faster response production. This is only true if you have the capacity to run multiple I/O calls concurrently. In such a scenario, the total execution time will be approximately equal to the duration of the longest single operation. However, a single asynchronous operation is unlikely to outperform its synchronous counterpart.

    Another motivation for making methods async is to free up threads to serve other requests while the current I/O call is being processed. This is more about better resource utilization than raw performance.

    In summary, if your concerns revolve around optimizing CPU/thread utilization, or if you have the capability to run multiple calls concurrently, then adopting async could be beneficial. On the other hand, if these conditions don’t apply, there may not be significant advantages to using asynchronous programming over synchronous programming.

    Speaking about the issue related disposing context, please provide more details on your code.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search