【问题标题】:WCF Concurrency & Multi Threaded ClientWCF 并发和多线程客户端
【发布时间】:2017-06-19 12:19:40
【问题描述】:

我有一个 WCF,多线程客户端使用它来处理并行请求的数量。不知何故,它开始共享两个不同请求/线程的数据;我尝试将InstanceContextModeConcurrencyMode 更改如下:

  • 默认 InstanceContextMode = PerSession ConcurrencyMode = Single
  • InstanceContextMode = PerCall ConcurrencyMode = Single
  • InstanceContextMode = PerCall ConcurrencyMode = Multiple

但是到目前为止还没有运气。没有可能导致此并发问题的共享变量和函数。根据 MS 文档 PerCall 为每个调用创建实例,当实例拥有自己的私有内存堆栈时,如何在实例之间共享数据没有任何意义。

工作流程: WCF 有三个主要组件,.Net Assembly 1 和 .Net Assembly 2。WCF 有一个名为“Process”的函数,它接受一个字符串参数并根据通过 WCF 完成的处理返回结构化对象。 WCF 中没有实例变量;函数中的所有内容。该函数对字符串进行少量处理,并将其转换为结构化对象,然后将其传递给Assembly 1的函数进行一些处理,然后使用反射的invoke方法将其传递给Assembly 2的函数。这是以某种方式混合两个不同并发调用的最终结果的整个过程。

如果有人能帮助解决这个问题,我将非常感激。

【问题讨论】:

  • 我们需要查看更多代码。没有共享变量时如何共享数据?
  • 无法在此处发布整个代码,但我可以让您了解代码的工作流程及其工作原理。

标签: c# .net wcf concurrency


【解决方案1】:

您想让这些请求并行执行,而不是像您那样按顺序执行吗?通过在配置文件中添加serviceThrottling,我对此没有任何问题。我的配置如下所示:

<system.serviceModel>
    <services>
      <service name="TeleEcgService.Service">
        <endpoint address="" binding="basicHttpBinding" contract="TeleEcgService.IService"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceThrottling maxConcurrentCalls="100" maxConcurrentSessions="1" maxConcurrentInstances="100"/>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
    <bindings>
      <basicHttpBinding>
        <binding maxReceivedMessageSize="50000000">
          <!--
          <security mode="Transport">
          </security>
          -->
        </binding>
      </basicHttpBinding>
    </bindings>
  </system.serviceModel>

如您所见,我最多可以有 100 个并行请求。我使用默认的InstanceContextModeConcurrencyMode

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 2010-11-25
    • 2012-09-26
    相关资源
    最近更新 更多