首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HttpContext Null错误

HttpContext Null错误
EN

Stack Overflow用户
提问于 2015-10-18 08:18:43
回答 1查看 176关注 0票数 0

我正在构建一个大量模仿教程here的应用程序。它本质上是一个在线商店应用程序,当用户到达站点时,它分配一个唯一的字符串Id,应用程序使用该Id来存储用户在数据库中选择的内容。该字符串标识用户的购物车。

我有一些使用D3.js的客户端JavaScript,它需要将一些信息发送回服务器。我已经添加了一个web服务(.asmx),它可以很好地接收数据,但是在接收到数据之后,服务器会在数据库中查找用户的信息,但不能重新生成唯一的Id。

本教程给了我一个函数,它返回字符串Id,并且在JavaScript调用asmx函数之前很好地工作。我不明白为什么只有在web服务运行之后才会出现这个错误。

获取Id的函数

代码语言:javascript
复制
    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();
    }

我得到的错误是:

代码语言:javascript
复制
Message:    "Object reference not set to an instance of an object."

我的asmx web服务文件

代码语言:javascript
复制
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;
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2015-10-18 08:50:11

我想通了。显然,默认情况下,web方法的会话支持是关闭的。你可以阅读更多关于它的here.

我将我的web服务方法更改为下面的方法,它现在可以很好地工作:

代码语言:javascript
复制
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;
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33193017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档