偶是WCF的新人。现在有一个状况就是说。客户端会给服务端发请求,然后服务端就要马上将结果返回给客户端。客户端有可能很多,如果过多就要排队。
那WCF在服务端的时候如何才能写成多线程呢?谢谢
在线等哈。

解决方案 »

  1.   

    WCF本身就是多线程的。你还要怎么个多线程法?
      

  2.   

    WCF服务本身就支持并发.
    它的多个线程是从服务器端的IOCP的线程池中分配出来,默认县城池大小是1000
      

  3.   

    不知道是什么原因,可能是我的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请问,该用哪个版本呢?有下载地址么?我没方向了呢。
      

  4.   

    服务端多线程可以有很多方法去实现
    你可以在你的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
        }
      

  5.   

    to 4楼:
    很可能就是beta版本的问题。微软的一贯传统都是会在产品正式release的时候将以前beta版本的命名空间彻底换一遍