2012-03-04 116 views
0

我有webservice,提供给用户。用户必须登录才能使用它。 我的问题是当用户通过Web服务上的WSLogin登录时,我想保存会话以便下次检查。与会话呼叫动态web服务

[WebMethod(EnableSession = true)] 
public string WSLogin(string username, string password) 
{ 
    // do something 
    string sID = Guid.NewGuid().ToString(); 
    Session.Add("SID", sID); // save session 
    return sID; // return to user to check next time 
} 

[WebMethod(EnableSession = true)] 
public string CallSomethingService(string sID) 
{ 
    string curSID = Session["SID"].ToString() // error, Object reference not set to an instance of an object. 
    // do some thing 
} 

我动态调用webservice。 感谢所有帮助

+0

能否请你用的,而不是会议Context.Session? – PraveenVenu 2012-03-04 06:52:00

回答

0

取而代之的是:

Session.Add("SID", sID); 

使用本:

HttpContext.Current.Session["SID"] = sSid; 

,而是这个:

string curSID = Session["SID"].ToString(); 

使用本:

string curSID = HttpContext.Current.Session["SID"] as string; 

并确保你有这个在你使用的语句:

using System.Web.SessionState; 
+0

HttpContext.Current.Session [“SID”]作为字符串 总是返回null,我不知道System.Web.SessionState使用什么方法? – 2012-03-04 16:39:54

+0

然后问题必须是当你在这一行的会话中设置值时:string curSID = Session [“SID”]。ToString();你可以检查并看看你是否有一致的值(sID,sSid,SID ...)并且在HttpContext.Current上加上监视,看看有没有什么东西。该行使用System.Web.SessionState;必须位于类库的顶部。 – frenchie 2012-03-04 17:44:54

+0

当调用“WSLogin”时,我用一个值创建Session [“SID”],下次我调用“CallSomethingService”时,检查Session [“SID”]的值,但Session [“SID”]不存在。我在HttpContext.Current上看了一下,但是在“CallSomethingService”中看到了HttpContext.Current.Session.Count always = 0 – 2012-03-04 20:30:51