简体   繁体   中英

Error using session in WCF Service

I am using the below sample function in wcf project. It's working fine internally. When i am calling this function from outside of the hosted service. It returns the error.

Object reference not set to be an instance of an object.

I find that line HttpContext.Current.Session["UserSession"] // Error Code

try
{
    if (HttpContext.Current.Session["UserSession"] != null)
    {

    }
    else
    {

    }
}
catch(Exception ex)
{
    return ex.Message;
}

But i want to user the session variable. Please help me to solve this.

Try this :

if (HttpContext.Current.Session.Contains("UserSession"))
{
  if (HttpContext.Current.Session["UserSession"] != null)
  {

  } 
} 

It means your session variable is empty. Try using cache instead of session as session value is different for all pc's

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM