对于每个客户端的,服务端是否为每个客户端有专门的“通道”?
目的:想在服务端记录下来客户端的访问记录(进入、各个操作、离开等信息),并将其执行的操作独立记录在各个客户端对应的日志中。
下面是代码:
契约
[ServiceContract] public interface IService { [OperationContract] string GetData(string value); }
服务
public class Service : IService { public string GetData(string value) { string result = string.Format("Enter:{0}\t{1}\t{2}", value, GetClientIP(), AppDomain.CurrentDomain.ToString()); Log(result); System.Threading.Thread.Sleep(500); Log("Leave:{0}\t{1}", value, GetClientIP()); return result; } }
客户端
System.Timers.Timer timer1 = new System.Timers.Timer(); timer1.Interval = 2000; timer1.Elapsed += delegate { while (true) { ChannelFactory<IService> channelFatory = GetChannelFactory<IService>("BasicHttpBinding_IService1"); IService1 calculator = channelFatory.CreateChannel(); try { Console.WriteLine(calculator.GetData(IP)); } finally { CloseConnection((calculator as ICommunicationObject)); } } }; timer1.Start();
最后证明:服务为每个客户端连接开辟了单独的通道。不知道我的理解正确不。。。。待续