Is it possible to have have asynchronous calls to a RESTful API hosted under asp.net?
I have a long running process. I want to trigger it to start via an http GET request and then check on its progress periodically. How do I do this using the new C#5 async / await syntax?
I have a detailed example in this question:
How do I call an asynchronous process from an asp.net hosted module using C#5 async / await?
I think you don't need to use await or async. Maybe you can start a new thread to run the long time task, and return a task id to the client immediately. And the client use this id to check the progress of the task.
Related
Creating an GET endpoint that generates upwards of 50,000 records, as this system is communicating with a message broker and where the work is done by another application layer to generate all the data. This process takes on average 30 seconds.
Can GET endpoints be async and when should an endpoint be async ?
My current async implementation for API is based on the Request Reply Pattern .
I have tried looking for other examples on this, found most async API's are POST requests. Cannot confirm if making an GET endpoint async breaks the common conventions.
I am working on an application where I have used async/await for every endpoint. My question here is how async/await handles multiple requests at the same time on the same API endpoint. For example, if I have an endpoint to save a user record and that endpoint has been hit by two different users at the same time, what will happen? Can somebody explain?
Here is the example code here I am registering a restaurant using async/await:
[HttpPost]
[Route("RegisterRestaurant")]
public async Task<IActionResult> RegisterRestaurant([FromBody] RegisterRequestDTO registerModel)
{
var response = await _uow.UserRepository.RegisterRestaurant(new DTO.ResponseDTO.GenericResponseDTO<RegisterRequestDTO> { Data = registerModel });
return Ok(response);
}
Everything is fine with this code. But what will happen if it is hit by multiple users from multiple places at same time?
My question here is how async/await handles multiple requests at the same time on the same API endpoint. For example, if I have an endpoint to save a user record and that endpoint has been hit by two different users at the same time what will happen? Can somebody explain?
This actually has to do with how ASP.NET works, not async/await.
When new requests come in, ASP.NET takes a thread from the thread pool, constructs any necessary instances (e.g., an instance of your controller), and executes the handler for that request. So, each request is independent by default, unless your code explicitly uses static or singleton instances.
async/await do allow threads to return to the ASP.NET thread pool more quickly, but the core mechanism of ASP.NET handling requests is unchanged.
Which user will hit this action method first he will Register the restaurant and mean while second user will wait until the first user await completes.
I recently started using Hangfire to handle my background jobs in an ASP.NET project.
My jobs are involving lots, lots (and lots) of HTTP calls using WebRequest (I may migrate to something else, but that's for later).
While browsing SO on this subject, I stumbled upon people who do async calls in hangfire.
Is there any point in using async/await tasks inside a Hangfire job ? I'm aware that it is a queue, so jobs won't execute concurrently, but since my project has no GUI, is there any reason I would use async/await to do my HTTP calls inside a Hangfire job ?
PS : The requests that are involving a long-running job use the Task "design pattern", so clients won't experience any timeout
You can absolutely use Hangfire to run async methods, and use awaits within those async methods.
The SO question you linked to was making a blocking Wait call on the method passed to Hangfire to be serialized and then later executed by the hangfire service.
This is very different from something like
hangfireClient.Enqueue(() => myClass.RunAsync());
where RunAsync is an async method that contains awaits.
public async Task<bool> RunAsync() {
//do your work
var subJob = new worker();
return await subJob.DoWork();
}
I am using spring boot for developing services in my application.
I have a scenario where-in the request submitted to the back-end would take some time to complete.
To avoid waiting the client I want to return the response immediately with a message your request has been accepted. The request would be in progress in a background thread.
I see Spring provides the #Async annotation which can be used to create a separate processing thread from the main thread and using that I am able to offload the processing in a separate thread.
What I want to do is when I return the initial response as accepted I also want to provide the client with a tracking key/token which the client can later use to check the status of the request.
Since there can be multiple clients who would be accessing the service there should be a way of uniquely identifying each client's request from another.
I see there is no such feature in Spring Async or Future which can return a tracking id as such.
One possibility I see it to put the Future returned in HttpSession and later use that to check for the status by the client. But, I prefer not to use HttpSession and want my services to be stateless.
Is there any way/approach I can accomplish my requirement.
Thanks,
BS
Generate the key before calling the Async method, and pass it to the method:
String key = generateUniqueKey();
callAsyncMethod(key);
return key;
The Async method will have to persist the status of the execution somewhere (let's call it dataStore). When the client asks for the status using the key, you look it up on the dataStore and return it.
We are making a web service call on some data updates to sync another database. This web service call takes up some response time. Would adding it in a thread help at all? Any downfalls of doing this? If the web service calls fails, it fails and that is it. It is like a fire and forget call.
You could use an Asynchronous Web Service call using asyncronous callbacks to prevent blocking of your main thread.
By making an asynchronous call to a Web service, you can continue to
use the calling thread while you wait for the Web service to respond.
This means users can continue to interact with your application
without it locking up while the Web service access proceeds.
From MSDN: Making Asynchronous Web Service Calls
If it's taking long enough to hang the user interface then calling it on another thread is the recommended thing to do.
In addition to Tudor's answer I would suggest that you start off by using the new Task class from .NET 4.0.from task parallel library. Example would be:
Task backgroundProcess = new Task(() =>
{
service.CallMethod();
});
I strongly advice against using Async Web Service calls (including making calls in separate threads) from a web app. Instead use alternate approach like Ajax, and make this webservice call from an Ajax Call instance. There is no easy approach in the web context to handle threading and Async calls.