我正在构建一个大量模仿教程here的应用程序。它本质上是一个在线商店应用程序,当用户到达站点时,它分配一个唯一的字符串Id,应用程序使用该Id来存储用户在数据库中选择的内容。该字符串标识用户的购物车。
我有一些使用D3.js的客户端JavaScript,它需要将一些信息发送回服务器。我已经添加了一个web服务(.asmx),它可以很好地接收数据,但是在接收到数据之后,服务器会在数据库中查找用户的信息,但不能重新生成唯一的Id。
本教程给了我一个函数,它返回字符串Id,并且在JavaScript调用asmx函数之前很好地工作。我不明白为什么只有在web服务运行之后才会出现这个错误。
获取Id的函数
public string GetVirusId()
{
//Line where I get the error
if (HttpContext.Current.Session[DescriptionSessionKey] == null)
{
if (!string.IsNullOrWhiteSpace(HttpContext.Current.User.Identity.Name))
{
HttpContext.Current.Session[DescriptionSessionKey] = HttpContext.Current.User.Identity.Name;
}
else
{
// Generate a new random GUID using System.Guid class.
Guid tempDescriptionId = Guid.NewGuid();
HttpContext.Current.Session[DescriptionSessionKey] = tempDescriptionId.ToString();
}
}
return HttpContext.Current.Session[DescriptionSessionKey].ToString();
}我得到的错误是:
Message: "Object reference not set to an instance of an object."我的asmx web服务文件
namespace Trojan
{
/// <summary>
/// Summary description for updateGraph
/// </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 updateGraph : System.Web.Services.WebService
{
[WebMethod]
public bool analyseGraph(int x)
{
bool B = true;
using (VirusDescription virus = new VirusDescription())
{
B = virus.updateGraph(x);
}
return B;
}
}
}发布于 2015-10-18 08:50:11
我想通了。显然,默认情况下,web方法的会话支持是关闭的。你可以阅读更多关于它的here.
我将我的web服务方法更改为下面的方法,它现在可以很好地工作:
namespace Trojan
{
/// <summary>
/// Summary description for updateGraph
/// </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 updateGraph : System.Web.Services.WebService
{
[WebMethod(EnableSession = true)]
public bool analyseGraph(int x)
{
bool B = true;
using (VirusDescription virus = new VirusDescription())
{
B = virus.updateGraph(x);
}
return B;
}
}
}https://stackoverflow.com/questions/33193017
复制相似问题