为什么WCF InstanceContextMode.PerSession无法通过https工作?

时间:2010-12-09 10:04:02

标签: https wcf wcf-binding

我遇到[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客户端=新Service1Client();
client.GetMyValue(); // ==>返回0
client.SetMyValue(1); // ==>返回1
client.GetMyValue(); // ==>返回1
client.SetMyValue(6); // ==>返回6
client.GetMyValue(); // ==>返回6

客户的

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]结果是:

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

现在,当我将服务配置为使用https并使用证书传输安全性InstanceContextMode.PerSession就像InstanceContextMode.PerCall一样。

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

Service1Client客户端=新Service1Client();
client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser,StoreName.My,X509FindType.FindByThumbprint,“3d6ca7a6ebb8a8977c958a3d8e4436337b273e4e”);
          client.GetMyValue(); // ==>返回0
            client.SetMyValue(1); // ==>返回1
            client.GetMyValue(); // ==>返回0
            client.SetMyValue(6); // ==>返回6
            client.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一样?我错误配置了什么?

1 个答案:

答案 0 :(得分:2)

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

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

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

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