标签:
注意:以后此系列博文如果无特别说明,解决方案都按之前的Client、Host、Service方式进行建立,客户端代理类采用工具Scvutil.exe生成,如果不清楚可以参考此系列之前的博文。
using System.ServiceModel; using System.Threading; namespace Service { [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Single)] public class SampleMethod:ISampleMethod { public string MethodOne(string msg) { OperationContext operationContext = OperationContext.Current; InstanceContext instanceContext = operationContext.InstanceContext; string info = "InstanceContext HashCode: " + instanceContext.GetHashCode().ToString(); System.Threading.Thread.Sleep(2000); return "You called MethodOne return message is: " + msg + "\n->" + info + "\n->ManagedThreadId:" +
Thread.CurrentThread.ManagedThreadId.ToString()+" "+System.DateTime.Now.ToString(); } public string MethodTwo(string msg) { OperationContext operationContext = OperationContext.Current; InstanceContext instanceContext = operationContext.InstanceContext; string info = "InstanceContext HashCode: " + instanceContext.GetHashCode().ToString(); System.Threading.Thread.Sleep(3000); return "You called MethodTwo return message is: " + msg + "\n->" + info + "\n->ManagedThreadId:" +
Thread.CurrentThread.ManagedThreadId.ToString() + " " + System.DateTime.Now.ToString(); } } }
客户端采用异步方式进行处理,所以Svcutil.exe必须生成异步客户端(svcutil.exe /out:f:\SampleMethodClient.cs /config:f:\App.confighttp://localhost:1234/SampleMethod /a /tcv:Version35),客户端参考代码如下:
using System; namespace Client { class Program { static void Main(string[] args) { SampleMethodClient client1 = new SampleMethodClient(); client1.MethodOneCompleted += new EventHandler<MethodOneCompletedEventArgs>(client1_MethodOneCompleted); client1.MethodOneAsync("Client1 called MethodOne"); client1.MethodTwoCompleted += new EventHandler<MethodTwoCompletedEventArgs>(client1_MethodTwoCompleted); client1.MethodTwoAsync("Client1 called MethodTwo"); SampleMethodClient client2 = new SampleMethodClient(); client2.MethodOneCompleted += new EventHandler<MethodOneCompletedEventArgs>(client2_MethodOneCompleted); client2.MethodOneAsync("Client2 called MethodOne"); client2.MethodTwoCompleted += new EventHandler<MethodTwoCompletedEventArgs>(client2_MethodTwoCompleted); client2.MethodTwoAsync("Client2 called MethodTwo"); Console.Read(); } static void client2_MethodOneCompleted(object sender, MethodOneCompletedEventArgs e) { Console.WriteLine(e.Result.ToString()); } static void client2_MethodTwoCompleted(object sender, MethodTwoCompletedEventArgs e) { Console.WriteLine(e.Result.ToString()); } static void client1_MethodOneCompleted(object sender, MethodOneCompletedEventArgs e) { Console.WriteLine(e.Result.ToString()); } static void client1_MethodTwoCompleted(object sender, MethodTwoCompletedEventArgs e) { Console.WriteLine(e.Result.ToString()); } } }
运行结果:
结果说明:
Client1处理MethodOne和MethodTwo的时间点不同,但是处理的实例上下文ID和线程ID是一致的,这验证了上面的组合处理模型,并且由于客户端产生了两个代理,
建立了两个会话,所以Client1和Client2的实例上下文ID不一致。
运行结果:
结果说明:
Client1处理MethodOne和MethodTwo的时间点不同,虽然处理的实例上下文ID一致,但线程ID是不一致的,这验证了上面的组合处理模型,并且由于客户端产生了两个代理,
建立了两个会话,所以Client1和Client2的实例上下文ID不一致。
标签:
原文地址:http://www.cnblogs.com/wangweimutou/p/4643272.html