0

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.

Xaruth
  • 4,034
  • 3
  • 19
  • 26
user3085540
  • 275
  • 3
  • 12
  • 27
  • 1
    `eles`? Wouldn't be `else`? And please read http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Soner Gönül Mar 14 '14 at 11:13
  • 1
    check http://stackoverflow.com/questions/8321269/how-to-use-session-in-wcf let us see your's service and web.config – Kamran Shahid Mar 14 '14 at 11:16

2 Answers2

0

Try this :

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

  } 
} 
Ajay
  • 6,418
  • 18
  • 79
  • 130
0

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

user2978233
  • 55
  • 1
  • 1
  • 6