为什么会话未保存在简单的hello world示例Web服务中

时间:2014-04-23 18:28:06

标签: c# asp.net web-services session

我有以下网络服务:

[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服务引用而非服务引用。

enter image description here

注意:为了创建Web服务引用而不是Visual Studio为您添加的默认服务引用,我按照以下步骤操作。

在我的客户端控制台应用程序中我有:

var client = new Test();

client.SetSession();

bool isSessionSaved = client.IsSessionSaved();

为什么isSessionSaved = false

更新:如果我通过默认网站调用方法,我认为问题不在于它可以运行的Web服务。我确信客户端没有保存cookie。也许我需要一个cookie感知客户端。我可能需要修改客户端。

2 个答案:

答案 0 :(得分:1)

会话状态运行会话cookie,以支持您需要向您的soap客户端添加cookie容器的cookie。

    CookieContainer cookieJar = new CookieContainer();
    client.CookieContainer = cookieJar;

在WCF服务客户端上,等效的是在httpbinding配置上设置allowCookies =“true”。

答案 1 :(得分:0)

您必须在IIS上安装会话配置。

这是链接:http://technet.microsoft.com/en-us/library/cc725624(v=ws.10).aspx