WCF 第五章 一个单一实例中的多线程

默认的InstanceContextMode行为设置指导WCF为每个请求创建一 个新的服务实例。然后在很多情况下,这不是最好的解决方案。例如,如果一个服务有一个代价很高的例行初始化(比如,一个构造器从一个数据库读取数据或者创 建一个大的内存结构),它对每个服务请求的实例创建都不是很高效。为了创建一个被当前线程共享的单一服务实 例,InstanceContextMode.Single应该与ConcurrencyMode.Multiple结合使用。 InstanceContextMode.Single意味着只能有一个实例被创建,而ConcurrencyMode.Multiple设置指导WCF 在那个实例中同时执行多个线程。这可以提供一个重要的扩展性改进,但是服务代码必须处理同步来保护TLS(本地线程空间)。
  列表5.3显示了使用InstanceContextMode.Single和ConcurrencyMode.Multiple 行为的服务端代码。注意在类上的ServiceBehavior属性,不是接口。这是因为ServiceBehavior属性修改了服务行为,而不是它的 契约。
  列表5.3使用InstanceContextMode.Single和ConcurrencyMode.Multiple的服务
01[ServiceContract]
02public interface IStockService
03{
04    [OperationContract]
05    double GetPrice(string ticker);
06}
07 
08[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
09public class StockService : IStockService
10{
11    StockService()
12    {
13        Console.WriteLine("{0}:Created new instance of StockService on thread", DateTime.Now);
14    }
15    public double GetPrice(string ticker)
16    {
17        Console.WriteLine("{0}: GetPrice called on thread {1}", DateTime.Now, Thread.CurrentThread.ManagedThreadId);
18        Thread.Sleep(5000);
19        return 94.85;
20    }
21}
  图片5.3显示了客户端(左边)和服务端(右边)的输出结果。与先前的例子一样,客户端输出显示三个请求同步发送而且结果在5秒钟后返回。 服务端输出显示只有一个服务类的实例被创建,但是客户端请求仍然在它自己的线程中执行。InstanceContextMode.Single指导WCF 只创建一个服务类的实例,而ConcurrencyMode.Multiple设置指导WCF允许多个线程在实例中并发的执行。


=============

转载自

 

posted @ 2011-06-30 09:41  Gavin Liu  阅读(404)  评论(0编辑  收藏  举报

Right people get the right information at the right time.
以技术求生存,以市场求发展;学以至用,开拓创新;达技术之颠峰,至市场之广阔!