为什么WCF InstanceContextMode.PerSession无法通过https工作? [英] Why WCF InstanceContextMode.PerSession is not working over https?

查看:159
本文介绍了为什么WCF InstanceContextMode.PerSession无法通过https工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]问题

I have problems with [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession )]

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

I have simple wcf service, which is hosted in 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)]客户端的结果是:

Everything works if service site uses http. For example [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession )] result for client is:

Service1Client client = new Service1Client();

client.GetMyValue(); // ==>返回0

client.SetMyValue(1); // ==>返回1

client.GetMyValue(); // ==>返回1

client.SetMyValue(6); // ==>返回6

client.GetMyValue(); // ==>返回6

Service1Client client = new Service1Client();
client.GetMyValue(); //==> returns 0
client.SetMyValue(1); //==> returns 1
client.GetMyValue(); //==> returns 1
client.SetMyValue(6); //==> returns 6
client.GetMyValue(); //==> returns 6

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

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall )] result for client is:

Service1Client client = new Service1Client();

client.GetMyValue(); // ==>返回0

client.SetMyValue(1); // ==>返回1

client.GetMyValue(); // ==>返回0

client.SetMyValue(6); // ==>返回6

client.GetMyValue(); // ==>返回0

Service1Client client = new Service1Client();
client.GetMyValue(); //==> returns 0
client.SetMyValue(1); //==> returns 1
client.GetMyValue(); //==> returns 0
client.SetMyValue(6); //==> returns 6
client.GetMyValue(); //==> returns 0

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

Now when I configure my service to use https and transport security with certificate InstanceContextMode.PerSession acts like InstanceContextMode.PerCall.

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

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession )] result for client is now changed:

Service1Client client = new 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

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

我的服务web.config是:

My service web.config is:

<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一样?我错误配置了什么?

Why PerSession acts like PerCall? What have I misconfigured?

推荐答案

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

I got session support working over HTTPS.

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

WSHttpBinding does not support reliable sessions over transport security (HTTPS).

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

Instead of using wsHttpBinding, I created a custom binding:

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

这篇关于为什么WCF InstanceContextMode.PerSession无法通过https工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆