views:

120

answers:

2

I have a HttpHandler which queries 3 web services within a single request and store the results in a single cookie.

As you would imagine, the results collide. Here is how:

Process is as follows: when i query service 1 and waiting for the result, the cookie i m storing the results doesnt exist yet, then result comes from service 2 and volia, creates the cookie, stores the result, and then response comes back from service 1 and overwrites that cookie, which shouldnt be the case.

what i d like is to queue these requests.

Should i do it on client side via javascript? if yes, how ?:)

or do it on server side ?

so i dont want asynchronous calls. right?

here is the code:

if(service1.isEnabled){  
   invokeService1();  
}

if(service2.isEnabled){  
   invokeService2();  
}

if(service3.isEnabled){  
   invokeService3();  
}

invokeService1(){
  callToService1();
  // response comes to another HttpHandler, which is a redirect from the service      
}

invokeService2(){
  callToService2();  
  // response comes to another HttpHandler, which is a redirect from the service  
}

invokeService3(){
  callToService3();
  // response comes to another HttpHandler, which is a redirect from the service
}

when the responses arrive to the HttpHandler, it comes with querystring.

then on that HttpHandler:

HttpCookie cookie = request.Cookie.Get(MYCookie) ?? new HttpCookie(MYCookie);  

if(request.Params["foo"]){  
  //set cookie content  
}

if(request.Params["Bar"].isNotNullOrEmpty()){  
//set cookie content  
}

this is how i set it. The way i create the cookie is a problem I think.

A: 

Can you serialize the requests and make the client issue them indirectly?

for eg:

First request from client

GET /handler.ashx HTTP/1.1
....

Server recorgnizes that there is no cookie in the request. So, it assumes that this is a new connection. Server response with a redirect:

HTTP/1.1 302 Moved
Location: http://server.com/handler.ashx?executeService1

Second request from client

Now the client sends a request to handler.ashx?executeService1

THe handler gets this request and queries WebService1. And puts a cookie in the response (with another redirect)

HTTP/1.1 302 Moved
Location: http://server.com/handler.ashx?executeService2
set-cookie: value

You get the idea?

The advantage is that the server is on the handshake with the client. SErver knows how many times the web services have to be called and can use the cookie to do that.

Another option as you suggested is to do on the client side using javascript and AJAX with an XML payload. When the webservice calls are done, you can then call the server with the result of the service calls, and server can set a cookie on the client.

feroze
+1  A: 

No, everything becomes much simpler if you rely on asynchronous calls, so you have to provide callback functions to your invokeService and callService methods, and then continue execution on the callback.

Try this:

var cookieData = {};

var callService = function(service, onCalledService) {
    // TODO #1: contact the server to get the cookie data
    // on server callback:
    //     TODO #2:  append the retrieved data to cookieData
    //     and still inside the server callback do: onCalledService();       
};

var invokeService = function(service, onInvokedService) {
    if (service.isEnabled) {
        callService(service, function() {
            onInvokedService();
        });
    } else {
        onInvokedService();
    }
};

invokeService(service1, function () {

    invokeService(service2, function () {

        invokeService(service3, function () {
            // cookieData now has all the data you need, so:
            // TODO #3: set the cookie content
        });

    });

});

This is what should happen:

1. invokeService(service1, ...) will check if service1 is enabled
1.1. if service1 is enabled 
1.1.1. callService(service1, ...)
1.1.2. append the retrieved data to cookieData
1.1.3. call the callback (i.e. go to 2.)
1.2. if service1 is not enabled 
1.2.1. call the callback (i.e. go to 2.)

2. invokeService(service2, ...) will check if service2 is enabled
2.1. if service2 is enabled 
2.1.1. callService(service2, ...)
2.1.2. append the retrieved data to cookieData
2.1.3. call the callback (i.e. go to 3.)
2.2. if service2 is not enabled 
2.2.1. call the callback (i.e. go to 3.)

3. invokeService(service3, ...) will check if service3 is enabled
3.1. if service3 is enabled 
3.1.1. callService(service3, ...)
3.1.2. append the retrieved data to cookieData
3.1.3. call the callback (i.e. go to 4.)
3.2. if service3 is not enabled 
3.2.1. call the callback (i.e. go to 4.)

4. set the cookie content
ivo
the calls are being made from server side code not from a javascript.