WCF,关于服务端多线程的问题 偶是WCF的新人。现在有一个状况就是说。客户端会给服务端发请求,然后服务端就要马上将结果返回给客户端。客户端有可能很多,如果过多就要排队。那WCF在服务端的时候如何才能写成多线程呢?谢谢在线等哈。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 WCF本身就是多线程的。你还要怎么个多线程法? WCF服务本身就支持并发.它的多个线程是从服务器端的IOCP的线程池中分配出来,默认县城池大小是1000 不知道是什么原因,可能是我的WCF版本问题吧。我用的是Visual Studio 2005 extensions for .NET Framework 3.0 (WCF & WPF).msi但是,我照着书的列子上写的时候,报错说base.InnerProxy.Request(cltMsg);********************************************************************************************************错误 2 “System.ServiceModel.DuplexClientBase<IChatDuplex>”并不包含“InnerProxy”的定义 C:\Documents and Settings\Reinhard\My Documents\Visual Studio 2005\Projects\DuplexWin\DuplexWinClient\ChatDuplexProxy.cs 69 14 DuplexWinClient****************************************************************************************************我下载了framework3.5 beat2。但是里面只有System.ServiceModel.Web.Dll而不是System.ServiceModel.Dll请问,该用哪个版本呢?有下载地址么?我没方向了呢。 服务端多线程可以有很多方法去实现你可以在你的SERVICE LIB 实现类前面加上属性使得服务类对每一个请求都创建一个新的实例去处理.例如:[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)] public class BNVII_ClientInfoService : BNVII.ServiceLib.IBNVII_ClientInfoService { //实现服务的一些方法...... }你还可以使用WCF的异步模式来实现服务器的并发处理例如:public class SYSLogWrite : BNIV.ServiceLib.ISYSLogWrite { public void WriteLogA(LogModel model) { try { SYSLog sl = new SYSLog(); DoWriteLogHanndler WorkHandler = new DoWriteLogHanndler(sl.DoWriteLog); AsyncCallback ac = new AsyncCallback(WriteLogComplete); IAsyncResult ir = WorkHandler.BeginInvoke(model, ac, WorkHandler); } catch { //throw ex; } } private void WriteLogComplete(IAsyncResult ir) { try { DoWriteLogHanndler caller = (DoWriteLogHanndler)ir.AsyncState; caller.EndInvoke(ir); } catch { //throw ex; } } private delegate void DoWriteLogHanndler(BNIV.Models.LogModel model);}你还可以使用多线程或异步编程在一个服务类的实例上实现并发处理例如:[ServiceBehaviorAttribute(InstanceContextMode = InstanceContextMode.Single)] public class TrackService : ITrackService { Dictionary<Guid, TracingCallBack> Clients = new Dictionary<Guid, TracingCallBack>(); Dictionary<Guid, TracingCountBLL> TCBs = new Dictionary<Guid, TracingCountBLL>(); public TrackService() { Events.TracingBySystemCountEvent += new Events.TracingBySystemCountEventHendler(Events_TracingBySystemCountEvent); } void Events_TracingBySystemCountEvent(Guid guid, object TracingCount) { if (Clients.ContainsKey(guid)) { if (TCBs.ContainsKey(guid)) { try { Clients[guid].SendTracingCount((List<BNIV.Models.TracingCountModel>)TracingCount); } catch { Clients.Remove(guid); TCBs.Remove(guid); } } } } #region ITrackService 成员 TracingCallBack CallBack { get { return OperationContext.Current.GetCallbackChannel<TracingCallBack>(); } } //开始异步处理 public void BeginTracingByServiceCount(Guid UID, int Frequency) { lock (Clients) { Events.FireShowMessageEvent(string.Format("{0} Begin Tracing.",UID.ToString())); Clients.Add(UID, CallBack); //TracingCountBLL会创建一个新的线程去处理客户端请求. TCBs.Add(UID, new TracingCountBLL(UID, Frequency)); } } //处理完成 public void EndTracingByServiceCount(Guid UID) { lock (Clients) { Events.FireShowMessageEvent(string.Format("{0} End Tracing.", UID.ToString())); if (Clients.ContainsKey(UID)) { Clients.Remove(UID); } if (TCBs.ContainsKey(UID)) { TCBs[UID].UnRegisterEvent(); TCBs.Remove(UID); } } } #endregion } to 4楼:很可能就是beta版本的问题。微软的一贯传统都是会在产品正式release的时候将以前beta版本的命名空间彻底换一遍 急求!!! 鼠标滚轮事件问题 list《》怎么删除一项 在.Net开发中,如何查询相应配置属性的枚举类型有几种选项 (求助)VS 没有解决方案该怎么修改. 每日一学 我是菜鸟,想问问递归查询在文档查询中有什么意义 c#的web程序启动时,如何默认窗口最大化 我开了一个线程,然后让它去显示datagrid里面的内容,出现了异常的情况,下面是我的代码 关于tabcontrol的小问题,大家帮看看。 combobox数据绑定 [急] 谁能帮我看看这个div转table,很快的,只花1分钟给您送上20分。
它的多个线程是从服务器端的IOCP的线程池中分配出来,默认县城池大小是1000
报错说
base.InnerProxy.Request(cltMsg);********************************************************************************************************
错误 2 “System.ServiceModel.DuplexClientBase<IChatDuplex>”并不包含“InnerProxy”的定义 C:\Documents and Settings\Reinhard\My Documents\Visual Studio 2005\Projects\DuplexWin\DuplexWinClient\ChatDuplexProxy.cs 69 14 DuplexWinClient
****************************************************************************************************我下载了framework3.5 beat2。但是里面只有System.ServiceModel.Web.Dll而不是System.ServiceModel.Dll请问,该用哪个版本呢?有下载地址么?我没方向了呢。
你可以在你的SERVICE LIB 实现类前面加上属性使得服务类对每一个请求都创建一个新的实例去处理.
例如:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class BNVII_ClientInfoService : BNVII.ServiceLib.IBNVII_ClientInfoService
{
//实现服务的一些方法......
}
你还可以使用WCF的异步模式来实现服务器的并发处理
例如:
public class SYSLogWrite : BNIV.ServiceLib.ISYSLogWrite
{
public void WriteLogA(LogModel model)
{
try
{
SYSLog sl = new SYSLog();
DoWriteLogHanndler WorkHandler = new DoWriteLogHanndler(sl.DoWriteLog);
AsyncCallback ac = new AsyncCallback(WriteLogComplete);
IAsyncResult ir = WorkHandler.BeginInvoke(model, ac, WorkHandler);
}
catch
{
//throw ex;
} } private void WriteLogComplete(IAsyncResult ir)
{
try
{
DoWriteLogHanndler caller = (DoWriteLogHanndler)ir.AsyncState;
caller.EndInvoke(ir);
}
catch
{
//throw ex; }
}
private delegate void DoWriteLogHanndler(BNIV.Models.LogModel model);
}你还可以使用多线程或异步编程在一个服务类的实例上实现并发处理
例如:
[ServiceBehaviorAttribute(InstanceContextMode = InstanceContextMode.Single)]
public class TrackService : ITrackService
{
Dictionary<Guid, TracingCallBack> Clients = new Dictionary<Guid, TracingCallBack>();
Dictionary<Guid, TracingCountBLL> TCBs = new Dictionary<Guid, TracingCountBLL>();
public TrackService()
{
Events.TracingBySystemCountEvent += new Events.TracingBySystemCountEventHendler(Events_TracingBySystemCountEvent);
}
void Events_TracingBySystemCountEvent(Guid guid, object TracingCount)
{
if (Clients.ContainsKey(guid))
{
if (TCBs.ContainsKey(guid))
{
try
{
Clients[guid].SendTracingCount((List<BNIV.Models.TracingCountModel>)TracingCount);
}
catch
{
Clients.Remove(guid);
TCBs.Remove(guid);
}
}
}
}
#region ITrackService 成员
TracingCallBack CallBack
{
get
{
return OperationContext.Current.GetCallbackChannel<TracingCallBack>();
}
} //开始异步处理
public void BeginTracingByServiceCount(Guid UID, int Frequency)
{
lock (Clients)
{
Events.FireShowMessageEvent(string.Format("{0} Begin Tracing.",UID.ToString()));
Clients.Add(UID, CallBack);
//TracingCountBLL会创建一个新的线程去处理客户端请求.
TCBs.Add(UID, new TracingCountBLL(UID, Frequency));
}
}
//处理完成
public void EndTracingByServiceCount(Guid UID)
{
lock (Clients)
{
Events.FireShowMessageEvent(string.Format("{0} End Tracing.", UID.ToString()));
if (Clients.ContainsKey(UID))
{
Clients.Remove(UID);
}
if (TCBs.ContainsKey(UID))
{
TCBs[UID].UnRegisterEvent();
TCBs.Remove(UID);
}
}
} #endregion
}
很可能就是beta版本的问题。微软的一贯传统都是会在产品正式release的时候将以前beta版本的命名空间彻底换一遍