C# 会话变量在不同的Web服务方法中返回空值
我必须使用webmethod,我想在其中使用会话变量C# 会话变量在不同的Web服务方法中返回空值,c#,web-services,session,C#,Web Services,Session,我必须使用webmethod,我想在其中使用会话变量 [WebMethod(enableSession: true)] public List<int> FirstMethod() { List<int> ListValue=getListValue(); Session["ListValue"]=ListValue; return ListValue; //It works fine and it also return List of integer
[WebMethod(enableSession: true)]
public List<int> FirstMethod()
{
List<int> ListValue=getListValue();
Session["ListValue"]=ListValue;
return ListValue;
//It works fine and it also return List of integers which is ok
}
[WebMethod(enableSession: true)]
public List<int> SecondMethod()
{
if(Session["ListValue"]!=null)//null value in session :(
{List<int> ListValue=(List<int>)Session["ListValue"];
return ListValue;
}
else
return null;
}
}
现在我想在另一个不起作用的方法中使用ListValue的这个会话值,它会返回null值,而不是像这样的List
[WebMethod(enableSession: true)]
public List<int> FirstMethod()
{
List<int> ListValue=getListValue();
Session["ListValue"]=ListValue;
return ListValue;
//It works fine and it also return List of integers which is ok
}
[WebMethod(enableSession: true)]
public List<int> SecondMethod()
{
if(Session["ListValue"]!=null)//null value in session :(
{List<int> ListValue=(List<int>)Session["ListValue"];
return ListValue;
}
else
return null;
}
}
请注意:尝试创建无状态服务,而不是有状态服务。顺序词:根本不要使用会话。你可以看一看希望它有帮助。我在这方面是新的,请解释我不能理解建议的urlanswer@L-三请详细说明无状态和有状态之间的区别。另见