• WCF 第五章 控制并发调用的数量


    当InstancingMode设置成Single时,WCF在宿主内创建一个单一的实 例,不考虑有多少客户端被创建。当ConcurrencyMode设置成Multiple时,WCF为每个请求创建一个线程(取决于系统上限)以实现服务 方法的并行执行。为了减少这个,maxConcurrentCalls行为控制有多少个并发调用可以激活。

      列表5.9 显示了一个使用InstanceContextMode.Single和ConcurrencyMode.Multiple的服务行为。服务操作花费20秒完成。

     列表5.9 使用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(20000);
    19        return 94.85;
    20    }
    21}

      列表5.10 显示了服务的app.config文件。maxConcurrentCalls行为设置成5,意味着在同一时间不可以再激活多于5个调用。

    列表5.10 使用maxConcurrentCalls控制并发

    01<?xml version="1.0" encoding="utf-8" ?>
    02<configuration>
    03    <system.serviceModel>
    04        <bindings />
    05        <behaviors>
    06            <serviceBehaviors>
    07                <behavior name="throttling">
    08                  <serviceThrottling maxConcurrentCalls="5"/>
    09                </behavior>
    10            </serviceBehaviors>
    11        </behaviors>
    12        <services>
    13            <service behaviorConfiguration="throttling" name="Services.StockService">
    14                <endpoint address="" binding="basicHttpBinding" contract="Services.IStockService" />
    15                <host>
    16                    <baseAddresses>
    17                        <add baseAddress="http://localhost:8000/stockservice" />
    18                    </baseAddresses>
    19                </host>
    20            </service>
    21        </services>
    22    </system.serviceModel>
    23</configuration>

    图片5.7显示了列表5.7中的客户端(左边)和服务端(右边)输出。在客户端,注意当程序启动时10次调用立即开始。在这10次调用中,5个结果 在20秒钟后返回而剩下的5个结果在另外20秒钟后返回。在服务端输出,注意只有一个实例被创建。也要注意5次对GetPrice的调用立刻执行,每个都 在它们自己的线程里执行。当这5个线程结束后,线程被重用同时客户端的顺序请求被处理。

    图片5.7 控制并发调用数量的输出结果


    ========

    转载自

     

  • 相关阅读:
    windows 服务中托管asp.net core
    asp.net core自定义端口
    asp.net core 2.1 部署IIS(win10/win7)
    Centos7 安装Tomcat并运行程序
    centos7 安装java运行环境
    linux(centos7) 常用命令和快捷键 持续验证更新中...
    CentOS7 设置yum源
    dotnet core 入门命令
    二项式系数学习笔记
    [容斥原理][莫比乌斯反演] Codeforces 803F Coprime Subsequences
  • 原文地址:https://www.cnblogs.com/llbofchina/p/2094056.html
Copyright © 2020-2023  润新知