ASP.NET + C#HttpContext.Current.Session为null(Inside WebService)

这就是我发起会议的方式

protected void Session_Start(object sender, EventArgs e) { HttpContext.Current.Session["CustomSessionId"] = Guid.NewGuid(); } 

在我的类库中的解决方案中,我正在访问它并获得nullexception:

 string sess = HttpContext.Current.Session["CustomSessionId"] ; 

这是我在web.config和app.config中的配置(在我的库中)

            

(app.config)中

根据您的意见,您似乎正在尝试访问Web服务中的会话。 Web服务是无状态的,它们应该是这样的。 如果您想违反此规则并使其成为有状态,您可以在经典的ASMX Web服务中启用会话,如下所示:

 [WebMethod(EnableSession = true)] public void SomeMethod() { ... invoke the method in your class library that uses Session } 

这就是说,在类库中使用HttpContext.Current是一种应该不惜一切代价避免的做法。