WCF 客户端接收缓存的响应
我编写了一些数据获取服务,它从主 Web 服务器获取一些实体,Web 服务器交付项目,客户端处理这些项目。
Web方法
[OperationContract]
public DataItem[] GetPendingItems(){
using(DBContext c = new DBContext()){
var q = c.DataItems.Where(x=>x.Attempts<4)
.OrderBy(x=>x.Attempts)
.ThenBy(x=>x.ItemID);
foreach(var item in q){
item.Attempt ++; // making sure item was sent from server
// log what was sent..
}
Utils.Log(DateTime.Now, items);
c.SaveChanges();
return q.ToArray();
}
}
现在客户端类似的代码已经以这种方式编写了。
while(true){
var items = wcfClient.GetPendingItems();
Utils.Log(DateTime.Now, items);
// do something with items..
}
现在问题是,
由服务器发送
10:10:10 1,2,3
10:10:11 4,5,6
10:10:11 7,8,9
由客户端接收
10:10:10 1,2,3
10:10:11 4,5,6
10:10:11 4,5,6 <-- this is the problem
wcfClient是一个全局WCF服务实例 现在我知道这可能是问题所在,因此我们正在尝试创建新的 WCFClient 实例并尝试查看它是否有效,但如果您注意到,在我的 while 循环中,调用是完全独立且同步的。
但仔细观察,这种情况很少发生,但看起来 WCF 客户端返回了旧响应,而不是从服务器获取的响应。
现在是否有任何设置可以解释为什么会发生这种情况,或者为什么 WCF 缓存会响应。
我们在两端都使用最新的.NET 4.0 框架。平均有 2-5% 的物品会因此被遗漏。
我在 WCF 方面没有很好的经验,但了解 WCF 的本质,它根本不应该缓存任何内容。即使它正在发送缓存的响应,WCF 也不应该在服务器上执行方法。
我假设所有请求都是 HTTP POST 请求,因为我们选择了 BasicHttpBinding。
更新
将客户端代码更改为以下可以解决问题,
while(true){
var wcfClient = new WCFClient();
var items = wcfClient.GetPendingItems();
Utils.Log(DateTime.Now, items);
// do something with items..
}
那么如果使用相同的客户端,为什么会发生这种情况?
I have written some data fetch service which fetches some entities from main web server, web server delivers the items and client processes the items.
Web Method
[OperationContract]
public DataItem[] GetPendingItems(){
using(DBContext c = new DBContext()){
var q = c.DataItems.Where(x=>x.Attempts<4)
.OrderBy(x=>x.Attempts)
.ThenBy(x=>x.ItemID);
foreach(var item in q){
item.Attempt ++; // making sure item was sent from server
// log what was sent..
}
Utils.Log(DateTime.Now, items);
c.SaveChanges();
return q.ToArray();
}
}
Now similar code on client side has been written in such way..
while(true){
var items = wcfClient.GetPendingItems();
Utils.Log(DateTime.Now, items);
// do something with items..
}
Now here is the problem,
Sent by Server
10:10:10 1,2,3
10:10:11 4,5,6
10:10:11 7,8,9
Received by Client
10:10:10 1,2,3
10:10:11 4,5,6
10:10:11 4,5,6 <-- this is the problem
wcfClient is a global WCF Service Instance
Now I know this could be the issue, so we are trying to create new WCFClient instance and trying to see if it works, but if you notice, in my while loop, the calls are completely independent and synchronous.
But looking more closely, this happens rarely, but looks like WCF client returns and old response back to us instead of what it did fetch from server.
Now is there any settings that can explain me why this happens, or why does WCF cache response at all.
We are using latest .NET 4.0 framework on both ends. On an average 2-5% items are missed this way.
I do not have good experience in WCF, but knowing nature of WCF, it should not cache anything at all. Even if it is sending cached response, WCF should not execute method on server.
I assume all are HTTP POST requests as we have chosen BasicHttpBinding.
UPDATE
Changing client code to following resolves the issue,
while(true){
var wcfClient = new WCFClient();
var items = wcfClient.GetPendingItems();
Utils.Log(DateTime.Now, items);
// do something with items..
}
So why does this occur if same client is used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据我的猜测,这个问题可能与 Linq 有关,而不是与 WCF 有关。
As per my guess, this issue may be related to Linq and not WCF.
将代码更改为以下内容可以解决该问题,
重用 WCFClient(Soap 客户端端点)会导致它发送缓存的数据,即使它不应该发送。
Changing code to following resolves the issue,
Reusing WCFClient (Soap Client Endpoint) causes it to send cached data, even when it should not.