2010-10-13 44 views
4

我有一個HttpHandler在單個請求中查詢3個Web服務並將結果存儲在一個cookie中。Web服務和瀏覽器調用同步

正如你想象的那樣,結果會相互衝突。這是如何:

過程如下: 當我查詢服務1,並等待結果,存儲結果的cookie不存在,然後結果來自服務2和volia,創建cookie,存儲結果,然後響應從服務1返回並覆蓋該cookie,而不應該是這種情況。

我喜歡的是排隊這些請求。

我應該在客戶端通過JavaScript做到這一點嗎?如果是的話,如何?:)

或做它在服務器端?

所以我不想異步調用。對?

這裏是代碼:

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 
} 

當響應到達的HttpHandler的,它帶有查詢字符串。

然後在此的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 
} 

這是我如何設置它。我認爲創建cookie的方式是一個問題。

+0

所以,如果我理解正確的話:你的網站調用'HttpHandler'。這HttpHandler調用三個Web服務,然後返回一個cookie到調用Web瀏覽器,正確?我們可以查看處理程序當前如何調用Web服務的代碼,以及Cookie如何設置? – SamStephens 2010-10-18 01:51:09

+0

編輯了這個問題,並添加了一些代碼。 – DarthVader 2010-10-18 13:33:35

回答

0

您可以序列化請求並使客戶端間接發佈它們嗎?

爲如:

首先要求從客戶

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

服務器recorgnizes有在請求沒有Cookie。所以,它假設這是一個新的連接。一個重定向服務器響應:從客戶

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

第二個請求現在客戶端發送到handler.ashx請求executeService1

處理程序得到這個請求,並查詢WebService1?並在響應中添加一個cookie(使用另一個重定向)

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

您明白了嗎?

優點是服務器與客戶端握手。 SErver知道必須調用Web服務多少次,並且可以使用Cookie來完成此操作。

您建議的另一種選擇是在客戶端使用JavaScript和AJAX以及XML負載進行操作。當web服務調用完成後,您可以使用服務調用的結果調用服務器,並且服務器可以在客戶端上設置cookie。

1

沒有,一切都變得如果依靠異步調用簡單得多,所以你必須提供回調函數您invokeServicecallService方法,然後繼續回調執行。

試試這個:

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 
     }); 

    }); 

}); 

這是應該發生的事情:

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 
+0

調用來自服務器端代碼而不是來自javascript。 – DarthVader 2010-10-22 21:14:30