1
votes

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

I have simple wcf service, which is hosted in IIS 7.

Service code:

[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;
    }

}

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

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 )] result for client is:

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

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

[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(); //==> returns 0
client.SetMyValue(1); //==> returns 1
client.GetMyValue(); //==> returns 0
client.SetMyValue(6); //==> returns 6
client.GetMyValue(); //==> returns 0

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>   

Why PerSession acts like PerCall? What have I misconfigured?

1

1 Answers

2
votes

I got session support working over HTTPS.

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

Instead of using wsHttpBinding, I created a custom binding:

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