2010-12-09 7 views
1

J'ai des problèmes avec [ServiceBehavior (InstanceContextMode = InstanceContextMode.PerSession)]Pourquoi WCF InstanceContextMode.PerSession ne fonctionne pas sur https?

J'ai service simple WCF, qui est hébergé dans IIS 7.

Code de service:

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

} 

Tout fonctionne si site de service utilise http. Par exemple [ServiceBehavior (InstanceContextMode = InstanceContextMode.PerSession)] résultat pour le client est:

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

[ServiceBehavior (InstanceContextMode = InstanceContextMode.PerCall)] résultat pour le client est:

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

Maintenant, lorsque je configure mon service pour utiliser https et la sécurité du transport avec le certificat InstanceContextMode.PerSession agit comme InstanceContextMode.PerCall.

[ServiceBehavior (InstanceContextMode = InstanceContextMode.PerSession)] résultat pour le client est maintenant changé:

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

Mon service web.config est:

<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> 

Pourquoi PerSession agit comme Percall? Qu'est-ce que j'ai mal configuré?

Répondre

2

La prise en charge des sessions fonctionne via HTTPS. WSHttpBinding ne prend pas en charge les sessions fiables sur la sécurité de transport (HTTPS).

Au lieu d'utiliser wsHttpBinding, je créé une reliure personnalisée:

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