开发者

Best practice when using WebMethods and session

I want to reduce postback in one of my application page and use ajax instead. I used the WebMethod to do so.. I have a static WebMethod that needs to access the session variables and modify. and on the client side, i am calling this method using jQuery. I tried accessing the session as follows:

[WebMethod]
    public static void TestWebMethod()
    {
        if (HttpContext.Current.Session["pitems"] != null)
        {
            log.Debug("Using the existing list");
            Product prod = (Product)HttpContext.Current.Session["pitems"];
            List<Configs> confs = cart.GetConfigs();
            foreach (Configs citem in confis)
            {
                log开发者_JAVA百科.Info(citem.Description);
            }


        }
        log.Info("Inside the method!");
    } 

The values are displayed correctly and seems to work.. but i would like to know if this practice is allowed as the method is a static methods and would like to know how it will behave if multiple people access the application.

I would also like to know how developers do these kind of tasks in ASP if this is not the right method.

Thanks in advance for your suggestions and ideas,

Abdel Olakara


Static methods are completely safe to use. The problem some people run into with the fields being shared across requests only occurs with static fields, because they are indeed shared within the same process.

I think your method is a good way of going about this. This is exactly what web methods are for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜