简体   繁体   English

会话变量,Web服务,ASP.NET和C#

[英]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. 我在ASP.NET的会话变量和Web服务代理对象方面遇到问题。 I can access any data I create inside the actual .asmx file, but adding data "Through" the session variable results in absolutely nothing happening. 我可以访问在实际的.asmx文件中创建的任何数据,但是“通过”会话变量添加数据绝对不会发生任何事情。

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. 客户在此文本框中输入标题,然后将其发送到Web服务。 The web service is called in the masterpage, and it grabs an array list full of the "titles" that the customer is requesting. 该Web服务在母版页中被调用,它获取一个数组列表,其中包含客户所请求的“标题”。

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. 问题是,每当我调用Web服务方法时,绝对没有任何反应。

The Code in question: 有问题的代码:

Basket.asmx Basket.asmx

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;
}

Global.asax Global.asax

    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) (global.asax中的所有其他内容均为默认设置)

Masterpage 母版页

This is the only relevant code in the masterpage, It calls the web service through the session variable. 这是母版页中唯一相关的代码,它通过会话变量调用Web服务。

    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");
}

Reservations.aspx Reservations.aspx

This page submits the actual "new" data to the web service. 该页面将实际的“新”数据提交到Web服务。 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. 工作原理:我在ASMX中输入的默认测试值可以很好地加载到文本框中。

So in short, can I use a web service proxy object in a session variable? 简而言之,我可以在会话变量中使用Web服务代理对象吗? If not, whats the best way to "share" this object? 如果不是,“共享”该对象的最佳方法是什么?

Also: I'm using VS2005. 另外:我正在使用VS2005。

Thanks for any help! 谢谢你的帮助!

Every web service call occurs on a different instance of the web service class. 每个Web服务调用都在Web服务类的不同实例上发生。 Your reservations variable cannot be used to maintain state between calls, since it's an instance variable. 您的reservations变量不能用作维护两次调用之间的状态,因为它是一个实例变量。

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. 这样,购物车不会因系统故障而丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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