我正在尝试在会话中保存数组并尝试将其恢复。 以下是代码。但是当我调用WebMethod时,我收到以下错误。我正在使用c#。 VS2010
错误:
System.NullReferenceException:未将对象引用设置为实例 一个对象。在xmlRW1.Service1.logic()中 C:\ Users \ uydarp \ Documents \ Visual Studio 2010 \ Projects \ xmlRW1 \ xmlRW1 \ Service1.asmx.cs:第86行
[WebMethod]
public int logic()
{
int[] myArray = { 1,2,3,4};
Session["MyArray"] = myArray;
int[] myArray2 = (int[])Session["MyArray"];
int firstElement = myArray2[0];
return firstElement;
}
答案 0 :(得分:4)
默认情况下,asmx
服务中禁用了SessionState。您可以通过更改WebMethod
属性来明确启用它来启用它:
[WebMethod(EnableSession = true)]
public int logic()
{
int[] myArray = { 1,2,3,4};
Session["MyArray"] = myArray;
int[] myArray2 = (int[])Session["MyArray"];
int firstElement = myArray2[0];
return firstElement;
}