user2677850
user2677850

Reputation: 1

Getting the immediate response from server without waiting to 200 message

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

Answers (3)

alex.b
alex.b

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

Sean Kornish
Sean Kornish

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:

  1. Client sends request to server
  2. Server accepts client data
  3. Server inserts the request data into database
    a. Windows Service monitors database request queue and processes
  4. Server responds to client with "200"

Upvotes: 2

Damith
Damith

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

Related Questions