2010-10-13 40 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