双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

WCF 心跳包导致server端性能差的问题

shilei_0124 shilei_0124 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
各位大神,我最近在设计一个wcf的程序,但是遇到了点问题。

小弟在设计一个基于观察者模式的程序,需要涉及到多个客户端和一个server端相连,所有客户端每隔5秒发个心跳包给server。在server端我用System.Collections.Concurrent.ConcurrentDictionary容器来保存对客户端连接的引用和客户端对应的那个心跳包。由于每个客户端会不停的发数据包,然后就会不停的去往这个容器里读写东西,我很担心会引起性能问题,有大神能给我解释下ConcurrentDictionary的锁的机制是什么样的么?

PS:或者,我是不是可以抛弃wcf,通过SOCKET来实现比较好一点。
不知道我描述清楚了么有,在此先谢谢各位大神了。

server端代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;
using Server;
using System.Collections.Concurrent;
using System.ServiceModel.Channels;


namespace WCFLib
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class Publisher : IPublisher
{
private static readonly ConcurrentDictionary<iCallBack, Package> Clients = new ConcurrentDictionary<iCallBack, Package>();
public void Register()
{
var client = OperationContext.Current.GetCallbackChannel<iCallBack>();
if (!Clients.ContainsKey(client))
{
Clients.TryAdd(client, new Package(DateTime.Now));
}

}

public void SendHeartbeatPackage()
{
var client = OperationContext.Current.GetCallbackChannel<iCallBack>();

if (Clients.ContainsKey(client))
{
Clients[client] = new Package(DateTime.Now);
}

Console.WriteLine("Begin to update heartbeat package.");
Console.WriteLine("Updated time is {0}", (Clients[client] as Package).Time);
}

}
}

client端代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Threading;
using System.Net;

namespace ConsoleClient2
{
class Program
{
static void Main(string[] args)
{
InstanceContext callback = new InstanceContext(new Callback());
ServerProxy.PublisherClient proxy = new ServerProxy.PublisherClient(callback);
proxy.Register();

new Thread(() =>
{
while (true)
{
Thread.Sleep(5000);
proxy.SendHeartbeatPackage();
}
}).Start();

Console.Read();
}
}
}

...全文

相关推荐

WCF netTcpBinding 双工通信 实时连接断开检测

WCF netTcpBinding 双工通信 实时连接断开检测的简单示例

zhangtirui

WCF 4.0 WS-Discovery服务自发现实战指南

服务发现是分布式系统的基础能力,指服务实例在运行时自动被其他组件识别与定位的技术机制。其核心原理在于解耦服务提供方与消费方的静态地址依赖,通过动态广播、监听与元数据交换实现网络层自治。相比UDDI等中心化注册方案,WS-Discovery以UDP组播为基础,具备去中心、无状态、低开销的技术价值,特别适用于工业现场、边缘设备、测试环境等局域网高鲁棒性场景。它天然集成于WCF 4.0,支持Hello/Bye生命周期管理、Scope作用域过滤和安全扩展,无需额外部署注册中心。本文围绕WS-Discovery协议在

ciyinzhi8788

SQL Server 2025 Windows 11安装全解析:系统兼容性与服务注册机制

SQL Server 是微软主流关系型数据库管理系统,其安装过程高度依赖操作系统底层支持。在Windows 11环境下,SQL Server 2025 引入了全新的服务注册机制、WMI命名空间初始化逻辑及安全模型适配要求,不再兼容旧版注册表路径与服务依赖配置。其技术价值体现在深度整合HVCI内核保护、AppContainer沙箱和Windows Defender Application Control(WDAC)策略,显著提升运行时安全性。典型应用场景包括电赛E题等本地时序数据处理、嵌入式开发环境快速部署,

de1981

使用MSMQ / Duplex WCF / SignalR / jQuery将实时结果流式传输到网站

使用MSMQ / Duplex WCF / SignalR / jQuery将实时结果流式传输到网站

weixin_38592420

remoting双向通信

remoting 双向通信 服务端可以向客户端广播发送消息,多个客户端可以收到,客户端也可以向服务端发送消息

zhangjingmza
评论 14
收藏 已收藏
分享
返回顶部
shilei_0124
C# 111,129 成员 642,540 内容