OperationContext.Current.SessionId geting null

时间:2016-06-06 08:58:46

标签: c# .net wcf

当我调用服务GetSessionId()时,会话ID变为空值。请帮我解决这个问题..

public Response<string> GetSessionId()
{
    Response<string> respn = new Response<string>();
    respn.Succeeded = false;

    try
    {
        _sessionId = OperationContext.Current.SessionId;
        respn.ReturnData = _sessionId;
        respn.Succeeded = true;
    }
    catch (Exception ex)
    {
        respn.Succeeded = false;
        respn.ErrorMsg = ex.Message;
    }
    return respn;
}

在Web.Config文件中如果我设置为安全模式=“消息”并且clientCredentialType =“Windows”,那么它将获得会话ID。但我不能使用任何安全性。

<wsHttpBinding>
    <binding name="SOAPService_EndpointBinding"
             maxReceivedMessageSize="2147483647"
             openTimeout="00:02:00"
             closeTimeout="00:02:00"
             sendTimeout="24:00:00"
             receiveTimeout="24:00:00">          
      <security mode="None">
        <message clientCredentialType="None"/>
      </security>
    </binding>
  </wsHttpBinding>

2 个答案:

答案 0 :(得分:0)

您使用的绑定是什么?以下绑定支持会话

  • NetTcpBinding的
  • NetNamedPipeBinding
  • WSHttpBinding(仅在启用安全性或可靠性时)

答案 1 :(得分:0)

解决方案如下,

在合约界面中,将SessionMode属性设置为必需

[ServiceContract(SessionMode = SessionMode.Required)]

在配置文件中将reliableSession设为true

<wsHttpBinding>
    <binding name="SOAPService_EndpointBinding"
             maxReceivedMessageSize="2147483647"
             openTimeout="00:02:00"
             closeTimeout="00:02:00"
             sendTimeout="24:00:00"
             receiveTimeout="24:00:00">          
      <security mode="None">
        <message clientCredentialType="None"/>
      </security>
      <reliableSession enabled="true"/>
    </binding>
  </wsHttpBinding>