You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET应用会话超时设置无效求助(WCF服务+SQL Server存储)

WCF服务会话超时问题的可行解决方案

1. 修正WCF绑定的会话超时配置

WCF自身会话与ASP.NET会话是独立的,需在web.config的<system.serviceModel>中明确配置绑定的超时参数,确保与ASP.NET会话超时同步:

<system.serviceModel>
  <bindings>
    <wsHttpBinding>
      <binding name="WSHttpBinding_IService" 
               receiveTimeout="00:30:00" 
               sendTimeout="00:30:00"
               openTimeout="00:01:00"
               closeTimeout="00:01:00">
        <!-- 启用可靠会话并设置无活动超时 -->
        <reliableSession enabled="true" inactivityTimeout="00:30:00" />
        <security mode="Message">
          <message clientCredentialType="Windows" />
        </security>
      </binding>
    </wsHttpBinding>
  </bindings>
  <services>
    <service name="YourNamespace.YourService">
      <endpoint address="" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService"
        contract="YourNamespace.IService" />
    </service>
  </services>
</system.serviceModel>

注意:inactivityTimeout需与ASP.NET会话超时值保持一致,比如均设为30分钟

2. 同步SQL会话状态的超时配置与清理机制

确保web.config中SQL会话状态的配置正确,同时验证SQL Server的会话清理作业正常运行:

(1)web.config配置

<system.web>
  <sessionState mode="SQLServer" 
               sqlConnectionString="Data Source=YourSQLServer;Initial Catalog=ASPState;Integrated Security=True"
               timeout="30" 
               allowCustomSqlDatabase="true" />
</system.web>

(2)SQL Server作业检查

登录SQL Server,找到ASPState数据库的ASPState_Job_DeleteExpiredSessions作业,确认其处于启用状态(默认每分钟执行一次),该作业负责清理超时的会话数据,若停止会导致会话超时不生效。

3. 启用WCF与ASP.NET会话集成

WCF默认不依赖ASP.NET会话,需显式开启集成:

(1)服务类添加特性

<ServiceContract()>
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Required)>
Public Class YourService
    Implements IService

    ' 服务方法中可直接访问HttpContext.Session
    Public Function GetData(ByVal value As Integer) As String Implements IService.GetData
        HttpContext.Current.Session("LastActivity") = DateTime.Now
        Return String.Format("You entered: {0}", value)
    End Function
End Class

(2)web.config启用兼容性

<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>

4. 自定义会话超时验证逻辑

若上述配置仍不生效,可在服务方法中主动添加超时检查:

Public Function GetData(ByVal value As Integer) As String Implements IService.GetData
    Dim lastActivity As DateTime? = TryCast(HttpContext.Current.Session("LastActivity"), DateTime)
    
    If lastActivity Is Nothing Then
        HttpContext.Current.Session("LastActivity") = DateTime.Now
    Else
        ' 检查是否超过设定的超时时间(示例为30分钟)
        If DateTime.Now.Subtract(lastActivity.Value).TotalMinutes > 30 Then
            HttpContext.Current.Session.Abandon()
            Throw New FaultException("会话已超时,请重新连接")
        Else
            HttpContext.Current.Session("LastActivity") = DateTime.Now
        End If
    End If

    Return String.Format("You entered: {0}", value)
End Function

5. 调整IIS应用池闲置超时

确保应用池的闲置超时时间大于等于会话超时时间,避免应用池提前回收导致会话丢失:

  • 打开IIS管理器,找到目标应用池→右键「高级设置」
  • 在「进程模型」中设置「闲置超时(分钟)」为大于会话超时的值(比如40分钟)

内容的提问来源于stack exchange,提问作者Dolly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 20:18:30