[英]Why is session not saved in a simple hello world example web service
我有以下Web服务:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Test : System.Web.Services.WebService
{
[WebMethod(EnableSession=true)]
public void SetSession()
{
HttpContext.Current.Session["Test"] = true;
}
[WebMethod(EnableSession = true)]
public bool IsSessionSaved()
{
var temp = HttpContext.Current.Session["Test"];
return temp != null;
}
}
注意我有EnableSession = true
。
在我的客户端上,我添加了Web服务参考,而不是服务参考。
注意 :为了创建Web服务引用而不是Visual Studio为您添加的默认服务引用,我遵循了以下步骤。
在我的客户端控制台应用程序中,我有:
var client = new Test();
client.SetSession();
bool isSessionSaved = client.IsSessionSaved();
为什么isSessionSaved = false
?
更新:如果我通过默认网站调用方法,我认为问题不在于Web服务是否起作用。 我确定客户端没有保存cookie。 也许我需要一个cookie感知客户端。 我可能需要修改客户端。
会话状态在会话cookie之外运行,为了支持cookie,您需要向cookie客户端添加cookie容器。
CookieContainer cookieJar = new CookieContainer();
client.CookieContainer = cookieJar;
在WCF服务客户端上,等效方法是在httpbinding配置上设置allowCookies =“ true”。
您必须在IIS上安装会话配置。
这是链接: http : //technet.microsoft.com/zh-cn/library/cc725624(v=ws.10).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.