日期:2014-05-18  浏览次数:20368 次

WebService调用Session问题,请有经验的指点一二。
Test.aspx.cs后台:
protected   void   Page_Load(object   sender,   EventArgs   e)
{
        if   (!this.IsPostBack)
                Session[ "Test "]   =   "This   is   a   test. ";
}

protected   void   Button1_Click(object   sender,   EventArgs   e)
{
          Test.localhost.GetData   gc   =   new   Test.localhost.GetData();
          gc.CookieContainer   =   new   System.Net.CookieContainer();
          Label1.Text   =   gc.GetSession( "Test ");  
}
*************************************************************************
WebService代码:
[WebMethod(Description= "调用客户端Session ",EnableSession=true)]
public   string   GetSession(string   SessionName)
{
            if   (Session[SessionName]   !=   null)
                    return   Session[SessionName].ToString();
            else
                    return   null;
}
######################################################################
现在的问题是pageload中成功赋值了session   ,但是这个webservice却调不出来,返回的始终是null,盼指点。

------解决方案--------------------
WebService不能调用Session 吧???

------解决方案--------------------
gc.CookieContainer = new System.Net.CookieContainer();


这是什么意思?

SERVICE的SESSION本来是和站点一致的
但是你NEW出来一份COOKIE 把SESSION连接KEY给丢了 你再去找 自然就没了
------解决方案--------------------
学习


------解决方案--------------------
1public class Class1
2 {
3 public string getSession(string sessionName){
4 System.Web.HttpContext context = System.Web.HttpContext.Current;
5
6 if(context.Session[sessionName]==null)
7 return " ";
8 else
9 return context.Session[sessionName].ToString();
10
11 }
12 }

------解决方案--------------------
lz有没想过web服务最初是设计为跨平台、跨应用调用的,而.net的Session作用域是IIS内的一个项目,web服务通过xml传递数据,是无法传递Session的,所谓的EnableSession应该是指在一个项目内部调用,如果lz的项目为Test,web服务类为GetData,可以通过以下调用获得Session值:
Test.GetData gc = new Test.GetData();
Label1.Text = gc.GetSession( "Test ");