我遇到ASP.NET會話變量和Web服務代理對象的問題。我可以訪問我在實際的.asmx文件中創建的任何數據,但通過添加數據「通過」會話變量導致完全沒有任何事情發生。會話變量,Web服務,ASP.NET和C#
我的目標很簡單,我想創建一個「幾乎購物車」。客戶在此文本框中輸入標題,並將其發送到Web服務。該Web服務在主頁面中調用,它抓取一個完整的客戶請求的「標題」的數組列表。
數據在一個下拉框中可見,以及一個存儲所有項目總成本的標籤(我現在不擔心成本)。
問題是,任何時候我打電話給Web服務方法,絕對沒有任何反應。
中的代碼問題:
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
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;
}
(在Global.asax的一切是默認值)
主頁面
這是主頁面中唯一相關的代碼,它通過會話變量調用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
此頁面提交實際的 「新」 數據給Web服務。我剪掉了很多這樣的東西(這是一個小組項目,所以我寫了很多代碼)。
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);
}
什麼工作:我在ASMX中輸入的默認測試值,它們加載到文本框中。
因此,簡而言之,我可以在會話變量中使用Web服務代理對象嗎?如果不是,什麼是「共享」這個對象的最佳方式?
另外:我正在使用VS2005。
感謝您的幫助!
同意。一個好的設計可以彌補這些問題。 –
好的,謝謝,我們幾乎已經通過數據庫實現了它,所以它不會很難實現,謝謝! – Alpaca