帮助将二维数组存储在Web服务的Cache中 [英] Help with 2-dimentional array to store in Cache for Web service
问题描述
我正在为电影院预订应用程序创建一个Web服务.我希望能够保留席位,行和名称.我使用了二维数组,并且希望将其存储在缓存中,以便可以保留座位和行.这是我的代码,但是给出了错误:
System.Web.Services.Protocols.SoapException:服务器无法处理请求. ---> System.NullReferenceException:对象引用未设置为对象的实例.
在System.Web.Caching.Cache.get_Item(字符串键)
在CinemaReservation \ CinemaReservation \ Service1.asmx.cs:第54行的CinemaReservation.Service1.ReserveSeat(ReserveSeatRequest req)中
service.asmx:
I am creating a web service for cinema reservation application. I want to able to reserve the seat, row and name for the reservation. I have used an 2-dimensional array and I want to able to store it in cache so the seat and row can be reserved. Here is my code, but its given an error:
System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at System.Web.Caching.Cache.get_Item(String key)
at CinemaReservation.Service1.ReserveSeat(ReserveSeatRequest req) in CinemaReservation\CinemaReservation\Service1.asmx.cs:line 54
The service.asmx:
namespace CinemaReservation
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
public enum Seat
{
seat1,
seat2,
seat3,
seat4
}
public enum Row
{
row1,
row2,
row3,
row4
}
public class ReserveSeatRequest
{
public string name;
public int row, seat;
}
public class ReserveSeatResponse
{
public string name;
public Seat seat;
public Row row;
}
[WebMethod]
public ReserveSeatResponse ReserveSeat(ReserveSeatRequest req)
{
ReserveSeatResponse resp = new ReserveSeatResponse();
Cache myCache = new Cache();
string[,] ArraySeat = (string[,])myCache[req.name];
if (ArraySeat == null)
{
ArraySeat = new string [req.row, req.seat];
myCache[req.name] = ArraySeat;
}else{//seat taken code}
resp.row = (Row)req.row;
resp.seat = (Seat)req.seat;
resp.name = req.name;
return resp;
}
}
}
The client application, form.cs:
namespace CinemaClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void rsvSeatButton_Click(object sender, EventArgs e)
{
ReserveSeatService.ReserveSeatRequest req =
new ReserveSeatService.ReserveSeatRequest();
req.name = nameTextBox.Text;
req.row = int.Parse(rowTextBox.Text) -1;
req.seat = int.Parse(seatTextBox.Text) - 1;
ReserveSeatService.Service1SoapClient client =
new ReserveSeatService.Service1SoapClient();
ReserveSeatService.ReserveSeatResponse resp =
client.ReserveSeat(req);
reserveNameText.Text = resp.name;
reserveRowText.Text = resp.row.ToString();
reserveSeatTextBox.Text = resp.seat.ToString();
}
}
}
有人请帮帮我
Please help me out, someone
推荐答案
您正在尝试从未初始化的catch(myCache)中获取价值.
Hi,
You are trying to get value from uninitialized catch (myCache).
Cache myCache = new Cache();
string[,] ArraySeat = (string[,])myCache[req.name];
谢谢,
我尝试初始化,但仍然是相同的错误.
Thanks,
I tried initializing, But still the same error.
Cache myCache = new Cache();
myCache[req.name] = null;
string[,] ArraySeat = (string[,])myCache[req.name];
这篇关于帮助将二维数组存储在Web服务的Cache中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!