InstanceContextMode.Single 可用于 WCF basicHttpBinding 吗?

is InstanceContextMode.Single available for WCF basicHttpBinding?

据我了解 this article:

Single: This will help us to share the data globally. We can create only one instance and the same instance will be reused on the subsequent calls. Same like Per Session this will work with all bindings except basicHttpBinding.

InstanceContextMode.Single 不适用于 basicHttpBinding。

但根据 this answer 它有效。

This article 增加了混乱。

我希望得到明确的答复和解释。

InstanceContextMode.Single 将强制 WCF 引擎在服务的整个生命周期内创建服务的单个实例 class。这意味着所有请求将共享同一个实例,而不是为每个请求创建一个实例。

使用 basicHttpBinding 完全可以做到这一点。

这是一个使用 basicHttpBinding 和 InstanceContextMode.Single:

的例子

首先是我的服务 class,它具有保留请求计数的私有字段:

using System.ServiceModel;

namespace WcfService1
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class Service1 : IService1
    {
        private int _singleCounter = 0;//this will be preserved across requests
        public Service1() 
        {
            //this constructor executes only ONCE
        }
        public string GetData()
        {
            //this will increment with each request 
            //because it is a SINGLE instance the count
            //will be preserved
            _singleCounter++;
            return string.Format("Requests on this instance: {0}", _singleCounter);
        }
    }
}

现在我的服务合同:

using System.ServiceModel;

namespace WcfService1
{    
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData();
    }
}

最后这是我的配置文件,使用 basicHttpBinding 进行单一绑定:

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- 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="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

现在使用 Visual Studio 附带的默认 WCF 测试客户端,结果如下:

第一次调用计数为 1:

第二次调用计数为 2:

点击几下后,计数仍在保留中:

我不确定为什么有些文章说 InstanceContextMode.Single 不受 basicHttpBinding 支持。这显然不是真的。

我一直将 InstanceContextMode.Single 与 ConcurrencyMode.Multiple 结合用于高吞吐量服务。

它的另一个优点是您可以在服务的生命周期内保留一些 "state" 可以在所有请求之间共享。例如,我将常用数据保存在私有字段中,以避免在每次请求时从数据库中加载它等。