简体   繁体   中英

Session Variables, Web Services, ASP.NET, and C#

I'm having an issue with ASP.NET's session variables and a web service proxy object. I can access any data I create inside the actual .asmx file, but adding data "Through" the session variable results in absolutely nothing happening.

My goal is quite simple, I want to create an "Almost Shopping cart". The customer enters a title into this text box, and it's sent to the Web Service. The web service is called in the masterpage, and it grabs an array list full of the "titles" that the customer is requesting.

The data is visible in a drop down box, and a label that stores the total cost of all the items (I'm not worried about the cost at the moment).

The issue is, anytime I call a web service method, absolutely nothing happens.

The Code in question:

public class basket  : System.Web.Services.WebService {

ArrayList reservations = new ArrayList();
double total = 0;

public basket()
{
    reservations.Add("Extreme Test Data");
    reservations.Add("Moar Test Data");
}

[WebMethod]
public string[] getReservations()
{
    //This may be part of the issue, still not sure.

    return (string[])reservations.ToArray(typeof( string));
}
[WebMethod]
public string toString()
{
    return reservations[reservations.Count - 1].ToString();
}


[WebMethod]
public double getTotal()
{
    return total;
}

[WebMethod]
public void addCost(double price)
{
    total = total + price;
}

[WebMethod]

public void addReservation(String title)
{

    reservations.Add(title);

}
[WebMethod]
public void removeReservation(string title)
{
}
[WebMethod]
public int getLength()
{
    return reservations.Count;
}

    void Session_Start(object sender, EventArgs e) 
{
    // Code that runs when a new session is started
    localhost.basket proxy = new localhost.basket();
    Session["reservations"] = proxy;
}

(Everything else in global.asax is default)

This is the only relevant code in the masterpage, It calls the web service through the session variable.

    protected void Page_Load(object sender, EventArgs e)
{

    localhost.basket proxy = (localhost.basket)Session["reservations"];

        lblTotal.Text = proxy.getTotal().ToString("c");
        string[] res = proxy.getReservations();
        ddReservations.DataSource = res;
        ddReservations.DataBind();
        proxy.addReservation("Half Life 2");
}

This page submits the actual "new" data to the web service. I've cut out a lot of this (It's a group project, so there's a lot of code I didn't write).

protected void Page_Load(object sender, EventArgs e)
{
    proxy = (localhost.basket)Session["reservations"];
    Response.Write(proxy.toString() + "Count: " + proxy.getLength());

}

  protected void cmdSubmit_Click(object sender, EventArgs e)
{
    proxy.addReservation(txtGameTitle.Text);        
    proxy.addCost(39.99);
}

What does work: The default test values I entered in the ASMX, they load fine into the textbox.

So in short, can I use a web service proxy object in a session variable? If not, whats the best way to "share" this object?

Also: I'm using VS2005.

Thanks for any help!

Every web service call occurs on a different instance of the web service class. Your reservations variable cannot be used to maintain state between calls, since it's an instance variable.

You're better off making your service be stateless. However, for a case like this, you should store the shopping cart into a database. That way, the cart won't be lost on a system failure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM