ASP.NET + C#HttpContext.Current.Session为空(里面的WebService) [英] ASP.NET + C# HttpContext.Current.Session is null (Inside WebService)
问题描述
这是我发起会话
protected void Session_Start(object sender, EventArgs e)
{
HttpContext.Current.Session["CustomSessionId"] = Guid.NewGuid();
}
在我的下一个类库解决方案,我正在triyng进行访问和获取零异常:
in my solution under a class library i am triyng to access it and getting null exception:
string sess = HttpContext.Current.Session["CustomSessionId"] ;
这是我在web.config中和的app.config配置(在我的库)
this is my config in web.config and app.config (in my library)
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>
<system.web>
<pages enableSessionState = "true" />
<httpModules>
<add type="System.Web.SessionState.SessionStateModule" name="Session"/>
</httpModules>
<compilation debug="true" targetFramework="4.0" />
</system.web>
(的app.config)
(app.config)
推荐答案
根据您的意见,似乎您试图在Web服务来访问会话。 Web服务是无状态的,这是他们应该的。如果要违背这个规律,让他们级状态,你可以使在这样一个经典的ASMX Web服务会话:
According to your comments it seems that you are trying to access the session in a web service. Web services are stateless and that's how they should be. If you want to violate this rule and make them stateful you could enable sessions in a classic ASMX web service like this:
[WebMethod(EnableSession = true)]
public void SomeMethod()
{
... invoke the method in your class library that uses Session
}
这是说,在类库使用 HttpContext.Current
是应该不惜一切代价避免一个非常做法。
This being said, using HttpContext.Current
in a class library is a very practice that should be avoided at all price.
这篇关于ASP.NET + C#HttpContext.Current.Session为空(里面的WebService)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!