WCF 彗星实现
我有一个需要在 Web 客户端(ASP.NET MVC)上进行实时更新的要求。我可以扭转它的唯一方法是实现COMET技术(ServerPush/Reverse-AJAX)技术。
场景是这样的: 用户A在不同的网站客户端中保存消息。然后,用户B将自动获取用户“A”在不同浏览器中所做的更新。
我实际上是通过这个架构完成解决方案的: ASP.NET MVC - 在 WCF 上执行 jquery ajax(post)请求(长池)。 WCF - 以 1 秒的间隔对数据库 (SQL Server) 进行一些轮询。如果新数据已添加到数据库中,则轮询将中断,数据将返回到客户端。
WCF COMET 方法伪代码:
private Message[] GetMessages(System.Guid userID)
{
var messages = new List<Message>();
var found = false;
/* declare connection, command */
while (!found )
{
try
{
/* open connection - connection.Open(); */
/* do some database access here */
/* close connection - connection.Close(); */
/* if returned record > 0 then process the Message and save to messages variable */
/* sleep thread : System.Threading.Thread.Sleep(1000); */
found = true;
}
finally
{
/* for sure, incase of exception */
/* close connection - connection.Close(); */
}
}
return messages.ToArray();
}
我关心和问题是:这是在 WCF 中执行轮询技术的最佳方法(间隔 1 秒)吗?
原因:我最大化了数据库连接池的使用,并且我我希望该技术没有问题。
注意:这是一个使用下面给出的 WCF 属性的多线程实现。
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall), ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = true)]
I have a requirement that needs a real-time updates on the Web client (ASP.NET MVC). The only way I can turn around on it is that to implement the COMET technique (ServerPush/Reverse-AJAX) technique.
The scenario is that:
User A save a message in different website client. Then, User B will automatically get the updates made by User "A" in different browser.
I actually finish the solution by this Architecture:
ASP.NET MVC - did a jquery ajax (post) request (long-pooled) on the WCF.
WCF - do some polling on the database (SQL Server) with the interval of 1 second. If new data has been added to the database, the polling is break with the data being returned on the client.
WCF COMET Method Pseudo Code:
private Message[] GetMessages(System.Guid userID)
{
var messages = new List<Message>();
var found = false;
/* declare connection, command */
while (!found )
{
try
{
/* open connection - connection.Open(); */
/* do some database access here */
/* close connection - connection.Close(); */
/* if returned record > 0 then process the Message and save to messages variable */
/* sleep thread : System.Threading.Thread.Sleep(1000); */
found = true;
}
finally
{
/* for sure, incase of exception */
/* close connection - connection.Close(); */
}
}
return messages.ToArray();
}
My concern and question is: Is it the best approach to do the polling technique in WCF (with 1 second interval)?
Reason: I maximized the use of database connection pooling and I am expecting that there is no issue on that technique.
Note: This is a multi-threaded implementation with the use of WCF given attributes below.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall), ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = true)]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议使用专用的实时服务器(即不在 IIS 中托管 WCF 服务)或使用托管服务进行实时更新。正如 Anders 所说,IIS 在处理多个长时间运行的并发请求方面并不是那么出色。
我还建议您考虑使用 WebSockets 的解决方案,并支持后备解决方案,例如 Flash、HTTP流式传输、HTTP 长轮询以及可能的轮询。 WebSocket 是客户端和服务器之间全双工双向通信的第一个标准化方法,最终将为任何此类问题提供更好的解决方案。
目前,实现您自己的 Comet/WebSockets 实时解决方案肯定是一项耗时的任务(您可能已经发现),特别是在构建面向公众的应用程序(可供使用多种不同浏览器的用户访问)时。
考虑到这一点,XSockets 项目看起来非常有趣,SuperWebSocket 项目。
我所知道的 .NET Comet 解决方案来自 FrozenMountain,它有一个用于 IIS 的 WebSync 服务器。还有PokeIn。
我编制了一份实时网络技术列表,这些技术也可能是有用。
I'd recommend using a dedicated realtime server (i.e. don't host the WCF service in IIS) or using a hosted service for realtime updates. As Anders says, IIS isn't all that great at handling multiple long-running concurrent requests.
I'd also suggest you look at using a solution which uses WebSockets with support for fallback solutions such as Flash, HTTP streaming, HTTP long-polling and then possibly polling. WebSockets are the first standardised method of full duplex bi-directional communication between a client and a server and will ultimately deliver a better solution to any such problems.
For the moment implementing your own Comet/WebSockets realtime solution is definitely going to be a time consuming task (as you may have already found), especially when building a public facing app where it could be accessed by users with a multitude of different browsers.
With this in mind the XSockets project looks very interesting as does the SuperWebSocket project.
.NET Comet solutions I know of are from FrozenMountain have a WebSync server for IIS. There is also PokeIn.
I've compiled a list of realtime web technologies that may also be useful.
您不能在更新时发送事件,而不是轮询数据库吗?无论如何,这就是我实现 Pub/Sub 场景的方式,而且效果很好。
nstead of polling the database cant you have an event sent when updating instead? Thats the way I've implemented Pub/Sub scenarios anyway and it works great.