【发布时间】:2012-09-16 16:51:34
【问题描述】:
我有一个启用了 wsHttpBindings 和 SSL 的 WCF 服务,但我想启用 WCF 会话。
将 SessionMode 更改为必需后
SessionMode:=SessionMode.Required
我收到如下所述的错误。
合同需要 Session,但绑定 'WSHttpBinding' 不支持 它或未正确配置以支持它。
这是我的示例应用程序。
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<client />
<bindings>
<wsHttpBinding>
<binding name="NewBinding0" useDefaultWebProxy="false" allowCookies="true">
<readerQuotas maxStringContentLength="10240" />
<!--reliableSession enabled="true" /-->
<security mode="Transport">
<transport clientCredentialType="None" proxyCredentialType="None" >
<extendedProtectionPolicy policyEnforcement="Never" />
</transport >
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="WcfServiceLib.TestService">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="NewBinding0"
contract="WcfServiceLib.ITestService">
<identity>
<servicePrincipalName value="Local Network" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="https://test/TestService.svc" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpsGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
ITestService.vb
<ServiceContract(SessionMode:=SessionMode.Required)>
Public Interface ITestService
<OperationContract(IsInitiating:=True, IsTerminating:=False)> _
Function GetData(ByVal value As Integer) As String
End Interface
TestService.vb
<ServiceBehavior(InstanceContextMode:=InstanceContextMode.PerSession, _
ReleaseServiceInstanceOnTransactionComplete:=False, _
ConcurrencyMode:=ConcurrencyMode.Single)>
Public Class TestService
Implements ITestService
Private _user As User
<OperationBehavior(TransactionScopeRequired:=True)>
Public Function GetData(ByVal value As Integer) As String _
Implements ITestService.GetData
If _user Is Nothing Then
_user = New User()
_user.userName = "User_" & value
_user.userPassword = "Pass_" & value
Return String.Format("You've entered: {0} , Username = {1} , Password = {2} ", _
value, _user.userName, _user.userPassword)
Else
Return String.Format("Username = {1} , Password = {2} ", _
_user.userName, _user.userPassword)
End If
End Function
End Class
我尝试了所有可能的解决方案,我可以找到,但没有任何帮助。
一些关于启用可靠会话的建议,但它不适用于 ssl(如果您有自定义绑定),其他建议使用 http 而不是 https,但如果可能的话,我想使用我当前的配置启用会话。
有什么方法可以做到这一点吗?
非常感谢任何形式的帮助。
【问题讨论】:
-
在 wcf 服务上启用会话状态的原因是什么?如果您正在寻找高性能,这可能是一个瓶颈。应避免使用会话状态 wcf 服务。
-
我想为每个会话设置一个 _user 变量。每次调用我都会得到一个新的服务类实例。 _user 的值始终为 Nothing。
-
如果我需要为每个客户存储数据,我应该针对我的情况使用什么解决方案?
-
您可以将用户变量存储在 wcf 请求自定义标头中并在服务器端读取它。或者您可以从请求中读取用户身份。这将为您完成这项工作。
-
我想将变量存储在服务器端,而不是将它们传递给客户端并返回。
标签: .net wcf session ssl https