【问题标题】:Why WCF InstanceContextMode.PerSession is not working over https?为什么 WCF InstanceContextMode.PerSession 不能通过 https 工作?
【发布时间】:2011-05-22 18:28:29
【问题描述】:

我对 [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession )] 有疑问

我有一个简单的 wcf 服务,它托管在 IIS 7 中。

服务代码:

[ServiceContract(SessionMode = SessionMode.Allowed)]
public interface IService1
{
    [OperationContract]
    int SetMyValue(int val);

    [OperationContract]
    int GetMyValue();
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession )]
public class Service1 : IService1
{
    int MyValue = 0;

    public int SetMyValue(int val)
    {
        MyValue = val;
        return MyValue;
    }

    public int GetMyValue()
    {
        return MyValue;
    }

}

如果服务站点使用 http,一切正常。 例如 [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession )] 客户端的结果是:

Service1Client 客户端 = new Service1Client();
客户端.GetMyValue(); //==> 返回 0
客户端.SetMyValue(1); //==> 返回 1
客户端.GetMyValue(); //==> 返回 1
客户端.SetMyValue(6); //==> 返回 6
客户端.GetMyValue(); //==> 返回 6

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall )] 客户端的结果是:

Service1Client 客户端 = new Service1Client();
客户端.GetMyValue(); //==> 返回 0
客户端.SetMyValue(1); //==> 返回 1
客户端.GetMyValue(); //==> 返回 0
客户端.SetMyValue(6); //==> 返回 6
客户端.GetMyValue(); //==> 返回 0

现在,当我将服务配置为使用 https 并使用证书传输安全性时,InstanceContextMode.PerSession 的行为类似于 InstanceContextMode.PerCall。

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession )] 客户端的结果现已更改:

Service1Client 客户端 = new Service1Client();
client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindByThumbprint, "3d6ca7a6ebb8a8977c958a3d8e4436337b273e4e");
客户端.GetMyValue(); //==> 返回 0
客户端.SetMyValue(1); //==> 返回 1
客户端.GetMyValue(); //==> 返回 0
客户端.SetMyValue(6); //==> 返回 6
客户端.GetMyValue(); //==> 返回 0

我的服务 web.config 是:

<bindings>
  <wsHttpBinding>
    <binding name="wsHttpEndpointBinding">
      <security mode="Transport">
        <transport clientCredentialType="Certificate"/>
      </security>
    </binding>
  </wsHttpBinding>
</bindings>

<services>
  <service behaviorConfiguration="ServiceBehavior" name="WcfServiceLibrary1.Service1">
    <endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttpEndpointBinding"
      name="wsHttpEndpoint" contract="WcfServiceLibrary1.IService1" />
  </service>
</services>

<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <serviceMetadata httpsGetEnabled="true" httpGetEnabled="false"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>   

为什么 PerSession 的行为类似于 PerCall?我配置错了什么?

【问题讨论】:

    标签: https wcf wcf-binding


    【解决方案1】:

    我获得了通过 HTTPS 工作的会话支持。

    WSHttpBinding 不支持基于传输安全 (HTTPS) 的可靠会话。

    我没有使用 wsHttpBinding,而是创建了一个自定义绑定:

    <customBinding>
      <binding configurationName="customReliableHttpBinding">
        <reliableSession />
        <textMessageEncoding/>
        <httpsTransport authenticationScheme="Anonymous" requireClientCertificate="true"/>
      </binding>
    </customBinding>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      • 2015-02-22
      • 1970-01-01
      • 1970-01-01
      • 2015-06-22
      • 1970-01-01
      相关资源
      最近更新 更多