.Net Framework CF版中没有Cookie容器,也能使用Session保存会话状态

博主在做一个采用Web服务的项目,客户端是WinCE 4.1+.Net Framework CF版。提到保存当前会话状态(如登录用户信息、操作单据等)用Session最佳,Web service也支持Session,一般应用中声明一个CookieContainer对象即可。

最近在做一个项目,这个项目是采用的Web服务,客户端是WinCE 4.1+.Net Framework CF版。大家知道,要保存当前会话状态(当前登录的用户信息,当前操作的单据等)最好是用Session来保存,Web service中也提供了session的支持。在一般的应用中使用Session是没有问题的,只需要把声明一个CookieContainer对象就可以了。如下:

None.gifService s=new Service();
None.gifs.CookieContainer
=new CookieContainer();
问题出现了,在.Net Framework 精简版中没有Cookie容器对象,难道就是说不能用Session了吗?那该如果保存会话状态呢?

经过几天的思考后,终于想到了一个解决办法:在服务器端用一个静态的HashTable模拟Session对象,用客户端的IP地址作为关键字,代码如下:
 1None.gifpublic class Global : System.Web.HttpApplication
 2ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 3InBlock.gif    private System.ComponentModel.IContainer components = null;
 4InBlock.gif    private static Hashtable UserSession;
 5InBlock.gif    private static int Session_timeout;                //Session超时时间
 6InBlock.gif
 7InBlock.gif        
 8InBlock.gif    public Global()
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
10InBlock.gif        InitializeComponent();
11ExpandedSubBlockEnd.gif    }
    
12InBlock.gif        
13InBlock.gif    protected void Application_Start(Object sender, EventArgs e)
14ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
15InBlock.gif        UserSession=new Hashtable();
16InBlock.gif        Session_timeout=60;
17ExpandedSubBlockEnd.gif    }

18InBlock.gif 
19InBlock.gif    protected void Session_Start(Object sender, EventArgs e)
20ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
21InBlock.gif        Session["ip"]=Request.UserHostAddress;
22InBlock.gif        
23InBlock.gif        //检测到该Ip是第一次访问或者Session已经超时,则把当前Session加入到HashTable中
24InBlock.gif        if(UserSession[Request.UserHostAddress]==null || DateTime.Compare(DateTime.Now,((DateTime)UserSession[Request.UserHostAddress+"_time"]).AddMinutes(Session.Timeout))>0)
25ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
26InBlock.gif            UserSession[Request.UserHostAddress]=Session;
27ExpandedSubBlockEnd.gif        }

28InBlock.gif        //保存最近一次访问时间
29InBlock.gif        UserSession[Request.UserHostAddress+"_time"]=DateTime.Now;
30ExpandedSubBlockEnd.gif    }

31InBlock.gif    
32InBlock.gif    //外部的函数可以通过这个函数读取Session
33InBlock.gif    public static System.Web.SessionState.HttpSessionState mySession(string IP)
34ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
35InBlock.gif        return (System.Web.SessionState.HttpSessionState)UserSession[IP];
36ExpandedSubBlockEnd.gif    }

37InBlock.gif    
38InBlock.gif    //检测当前HashTable保存的Session有没有已经超时的,有则清除掉
39InBlock.gif    private static bool checkSession(string HostAddress)
40ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
41InBlock.gif        try
42ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
43InBlock.gif            if(DateTime.Compare(DateTime.Now,((DateTime)UserSession[HostAddress+"_time"]).AddMinutes(Session_timeout).AddSeconds(-1))>0)
44ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
45InBlock.gif                UserSession.Remove(HostAddress);
46InBlock.gif                UserSession.Remove(HostAddress+"_time");
47InBlock.gif                return true;
48ExpandedSubBlockEnd.gif            }

49InBlock.gif            else
50ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
51InBlock.gif                return false;
52ExpandedSubBlockEnd.gif            }

53ExpandedSubBlockEnd.gif        }

54InBlock.gif        catch
55ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
56InBlock.gif            if(UserSession.ContainsKey(HostAddress))
57InBlock.gif                UserSession.Remove(HostAddress);
58InBlock.gif            return false;
59ExpandedSubBlockEnd.gif        }

60ExpandedSubBlockEnd.gif    }

61InBlock.gif    
62InBlock.gif    protected void Session_End(Object sender, EventArgs e)
63ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
64InBlock.gif        checkSession(Request.UserHostAddress);
65ExpandedSubBlockEnd.gif    }

66ExpandedBlockEnd.gif}

下面是调用代码:
 1None.gifpublic class Service : System.Web.Services.WebService
 2ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 3InBlock.gif    public Service()
 4ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 5InBlock.gif        InitializeComponent();
 6InBlock.gif
 7ExpandedSubBlockEnd.gif    }

 8ExpandedSubBlockStart.gifContractedSubBlock.gif    组件设计器生成的代码#region 组件设计器生成的代码
 9InBlock.gif        
10InBlock.gif    //Web 服务设计器所必需的
11InBlock.gif    private IContainer components = null;
12InBlock.gif                
13ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
14InBlock.gif    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
15InBlock.gif    /// 此方法的内容。
16ExpandedSubBlockEnd.gif    /// </summary>

17InBlock.gif    private void InitializeComponent()
18ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
19InBlock.gif
20ExpandedSubBlockEnd.gif    }

21InBlock.gif
22ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
23InBlock.gif    /// 清理所有正在使用的资源。
24ExpandedSubBlockEnd.gif    /// </summary>

25InBlock.gif    protected override void Dispose( bool disposing )
26ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
27InBlock.gif        if(disposing && components != null)
28ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
29InBlock.gif            components.Dispose();
30ExpandedSubBlockEnd.gif        }

31InBlock.gif        base.Dispose(disposing);        
32ExpandedSubBlockEnd.gif    }

33InBlock.gif        
34ExpandedSubBlockEnd.gif    #endregion

35InBlock.gif
36InBlock.gif    private object ReadCurrentSession(string Key)
37ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
38InBlock.gif        return Global.mySession(Session["ip"].ToString())[Key];
39ExpandedSubBlockEnd.gif    }

40InBlock.gif    private void SetCurrentSession(string Key,object Value)
41ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
42InBlock.gif        Global.mySession(Session["ip"].ToString())[Key]=Value;
43ExpandedSubBlockEnd.gif    }

44InBlock.gif    
45InBlock.gif    //测试方法1:使用Hashtable中保存的Session
46InBlock.gif    [WebMethod(EnableSession=true)]
47InBlock.gif    public int Add()
48ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
49InBlock.gif        int count;
50InBlock.gif        if(ReadCurrentSession("count")==null)
51InBlock.gif            count=0;
52InBlock.gif        else
53InBlock.gif            count=Convert.ToInt32(ReadCurrentSession("count"));
54InBlock.gif        count++;
55InBlock.gif        SetCurrentSession("count",count);
56InBlock.gif        return count;
57ExpandedSubBlockEnd.gif    }

58InBlock.gif    
59InBlock.gif    //测试方法2:使用系统内建的Session
60InBlock.gif    [WebMethod(EnableSession=true)]
61InBlock.gif    public int Add2()
62ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
63InBlock.gif        int count;
64InBlock.gif        if(Session["count2"]==null)
65InBlock.gif            count=0;
66InBlock.gif        else
67InBlock.gif            count=Convert.ToInt32(Session["count2"]);
68InBlock.gif        count++;
69InBlock.gif        Session["count2"]=count;
70InBlock.gif        return count;
71ExpandedSubBlockEnd.gif    }

72ExpandedBlockEnd.gif}

经过测试,以上代码完全可行,只是使用的时候要小心,因为同一个IP地址访问Web服务,即使是退出客户端应用程序,换个操作员重新登录,仍然会使用同一个Session,这就需要在服务器端判断,在适当的时候启用新的Session会话.

转载于:https://www.cnblogs.com/yiway/archive/2005/04/27/146454.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值