Reputation: 1
Is it possible to send the response (200) to the client immediately and process the request after the response send to the client?
Example: if I send a request to one page that will takes 2 min to response. I don’t want to wait 2 min to get the response (because I don’t need any response from the server) so I want immediate response from the server. I don’t worry about if the process will complete after the response send.
Anyone have an idea to implement this process?
Thank you.
Upvotes: 0
Views: 1170
Reputation: 4567
We do something like that in our project.
The basic idea is like Sean Kornish
suggests: server to accept client data and send the resonse to the client while scheduling the processing itself.
Here's the example console client
/wcf service
:
Client
class Program
{
static void Main(string[] args)
{
var client = new Service1Client();
for (int i = 0; i < 10000; i++)
{
string response = client.GetData(42);
Console.WriteLine("Response: '" + response + "'");
}
Console.ReadLine();
}
}
Service
public class Service1 : IService1
{
private static int _serviceState = 1;
public string GetData(int value)
{
new Task(DoHeavyLoadWork).Start();
return string.Format("The server state is {0}", _serviceState);
}
}
Please note that code is just an example so it's extremely dirty. But gives some hint, I hope.
Upvotes: 0
Reputation: 838
It sounds like the service you are writing for the web should make use of off-line process. Web communications work synchronously, so you don't want the client hanging around while the server is busy with their request. I would suggest a separate service running on your web server that you can offload the processes to. Perhaps something like this:
Upvotes: 2
Reputation: 63065
You can call Async
methods of your web service methods. it will not wait for responses.
for (int i = 0; i < 100; i++)
{
client.HelloWorldAsync();
}
Upvotes: 1