2010-12-09 37 views
1

我有問題[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]爲什麼WCF InstanceContextMode.PerSession不能通過https工作?

我有簡單的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 client = new 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?我有什麼錯誤配置?

回答

2

我會支持工作通過HTTPS。

WsHttpBinding的不支持通過傳輸安全可靠的會話(HTTPS)。

而不是使用的wsHttpBinding的,我創建了一個自定義綁定:

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