控制台客户端代码如下:
View Code
1 class Program
2 {
3 static void Main(string[] args)
4 {
5 for (int i = 0; i < 10; i++)
6 {
7 Thread thread = new Thread(new ThreadStart(DoWork));
8 thread.Start();
9 }
10 Console.ReadLine();
11 }
12
13 public static void DoWork()
14 {
15
16 string result;
17 Console.WriteLine("客户端线程" + Thread.CurrentThread.GetHashCode() + "开始:" + DateTime.Now.ToString("hh:mm:ss ms"));
18
19
20 ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
21 lock (client)
22 {
23 result = client.GetData();
24 Console.WriteLine("客户端线程" + Thread.CurrentThread.GetHashCode() + "结束:" + DateTime.Now.ToString("hh:mm:ss ms") + " 返回:" + result);
25 client.Close();
26 }
27 }
28
29 }
WCF服务代码:
View Code
1 namespace WCFLib
2 {
3 // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“Service1”。
4 [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false), CallbackBehavior(UseSynchronizationContext = false)]
5 public class Service : IService
6 {
7 Service()
8 {
9 Console.WriteLine("新的服务端实例被创建:", DateTime.Now);
10 }
11
12 public string GetData()
13 {
14 string iString;
15 DateTime iDateTime;
16
17 iDateTime = DateTime.Now;
18 iString = "服务端线程" + Thread.CurrentThread.ManagedThreadId + "创建:" + iDateTime.ToString();
19 Console.WriteLine(iString);
20 //Thread.Sleep(5000);
21 return iString;
22 }
23
24
25 }
26 }
运行结果:
我的问题是 为什么WCF端的线程不是10个不同的线程呢?
- 3 回答
- 0 关注
- 429 浏览
添加回答
举报
0/150
提交
取消