为什么 Web Service 中的 Session 不起作用?

作者:vkvi 来源:ITPOW(原创) 日期:2008-2-4

.aspx 调用 Web Service,其 WebMethod 中创建了一个 Session,可是再次访问该 .aspx 时再调用该 WebMethod 时,Session 丢失。为什么 Session 不起作用呢?

除了 Web Service 中应用 Session,调用时也需要用 Session,即将 WebService 对象保存在 Session 中,就会解决此问题,如下:

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["ws"] == null)
    {
        MyWebService ws = new MyWebService();
        ws.CookieContainer = new CookieContainer();
        Session["ws"] = ws;
    }
   
    MyWebService ws1 = (MyWebService)Session["ws"];
    //使用 ws1
}

有点笨拙,但也是没办法的办法,不过微软也表示正在改进 Web Service 中 Session 的问题。

相关阅读

相关文章