Webservice中使用Session、Application
在Asp.Net 2.0里,已经能够在WebMethod里使用 Session 、 Application 这种服务器变量了。
一、Session
[WebMethod(EnableSession = true)]
public string Hello()
{
return "Hello," + Session["user"];
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
[WebMethod(EnableSession = false)]
public string Hello1()
{
return "Hello," + Session["user"];
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
[WebMethod]
public string Hello2()
{
return "Hello," + Session["user"];
}
“EnableSession” 为 XML Web services 方法启用会话状态,启用为 true。默认状态为 false。
以上三种方式均能实现对Session变量的使用。但要注意:
如果状态设置为 true,则客户端访问WS时,可以不赋值Session变量,有默认值。
如果状态设置为 false,则客户端访问WS时,必须要先对Session变量赋值,否则报错。
二、Application
[WebMethod]
public string Hello3()
{
return "Hello," + Application["user"];
}
使用Application时,不需要设置方法标签
一、Session
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
“EnableSession” 为 XML Web services 方法启用会话状态,启用为 true。默认状态为 false。
以上三种方式均能实现对Session变量的使用。但要注意:
如果状态设置为 true,则客户端访问WS时,可以不赋值Session变量,有默认值。
如果状态设置为 false,则客户端访问WS时,必须要先对Session变量赋值,否则报错。
二、Application
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
使用Application时,不需要设置方法标签