ASP.NET의 세션 변수 및 웹 서비스 프록시 개체에 문제가 있습니다. 실제 .asmx 파일 내에서 만든 모든 데이터에 액세스 할 수 있지만 데이터를 추가하면 세션 변수가 "통과"되어 아무런 일도 일어나지 않습니다.세션 변수, 웹 서비스, ASP.NET 및 C#
내 목표는 간단합니다. "거의 쇼핑 카트"를 만들고 싶습니다. 고객이이 텍스트 상자에 제목을 입력하면 웹 서비스로 전송됩니다. 웹 서비스는 마스터 페이지에서 호출되며 고객이 요청하는 "제목"으로 가득 찬 배열 목록을 가져옵니다.
데이터는 드롭 다운 상자와 모든 항목의 총 비용을 저장하는 레이블 (현재 비용은 걱정하지 않음)에 표시됩니다.
웹 서비스 메서드를 호출 할 때마다 문제가 발생하지 않습니다.
문제의 코드 :
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에있는 다른 모든이 기본값)
Masterpage
이것은 마스터 페이지의 유일한 관련 코드이며 세션 변수를 통해 웹 서비스를 호출합니다.
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
이 페이지는 웹 서비스에 실제 "새로운"데이터를 제출합니다. 나는 이것을 많이 잘라 냈다. (이것은 그룹 프로젝트이므로 작성하지 않은 많은 코드가있다.)
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에 입력 한 기본 테스트 값으로, 텍스트 상자에 정상적으로로드됩니다.
간단히 말해서 세션 변수에 웹 서비스 프록시 객체를 사용할 수 있습니까? 그렇지 않다면이 객체를 "공유"하는 가장 좋은 방법은 무엇일까요?
기타 : VS2005를 사용하고 있습니다.
도움 주셔서 감사합니다.
동의. 좋은 디자인은 그러한 문제를 해결할 수 있습니다. –
좋아, 고마워, 우리는 이미 데이터베이스를 통해 구현되어있어 구현하기가 어렵지 않을 것이다. 감사합니다! – Alpaca