Web 服务和浏览器调用同步
我有一个 HttpHandler,它在单个请求中查询 3 个 Web 服务并将结果存储在单个 cookie 中。
正如您所想象的那样,结果会发生冲突。具体方法如下:
流程如下: 当我查询服务1并等待结果时,存储结果的cookie还不存在,然后结果来自服务2和volia,创建cookie,存储结果,然后响应从服务1返回并覆盖该cookie ,情况不应该如此。
id 喜欢的是将这些请求排队。
我应该通过 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的方式是一个问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,如果您依赖异步调用,一切都会变得更加简单,因此您必须为 invokeService 和 callService 方法提供回调函数,然后继续执行回调。
试试这个:
这应该发生:
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:
This is what should happen:
您可以序列化请求并让客户端间接发出它们吗?
例如:
来自客户端的第一个请求
服务器识别出请求中没有 cookie。因此,它假设这是一个新连接。服务器响应重定向:
来自客户端的第二个请求
现在客户端向 handler.ashx?executeService1 发送请求,
处理程序获取此请求并查询 WebService1。并在响应中放入一个 cookie(使用另一个重定向)
您明白了吗?
优点是服务器端与客户端握手。 SErver 知道必须调用 Web 服务多少次,并且可以使用 cookie 来执行此操作。
您建议的另一个选择是在客户端使用 javascript 和 AJAX 以及 XML 有效负载进行操作。当Web服务调用完成后,您可以使用服务调用的结果来调用服务器,并且服务器可以在客户端上设置cookie。
Can you serialize the requests and make the client issue them indirectly?
for eg:
First request from client
Server recorgnizes that there is no cookie in the request. So, it assumes that this is a new connection. Server response with a redirect:
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)
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.