Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice when using WebMethods and session

Tags:

ajax

asp.net

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.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

like image 306
Abdel Raoof Olakara Avatar asked Oct 15 '22 05:10

Abdel Raoof Olakara


1 Answers

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.

like image 193
Matti Virkkunen Avatar answered Nov 02 '22 22:11

Matti Virkkunen