C# 为什么会话没有保存在简单的hello world示例web服务中

C# 为什么会话没有保存在简单的hello world示例web服务中,c#,asp.net,web-services,session,C#,Asp.net,Web Services,Session,我有以下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

我有以下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服务引用,而不是VisualStudio为您添加的默认服务引用,我遵循以下步骤

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

var client = new Test();

client.SetSession();

bool isSessionSaved = client.IsSessionSaved();
为什么
isSessionSaved=false


更新:我不认为问题出在web服务中,如果我通过默认网站调用这些方法,它就会工作。我确信客户没有保存cookies。也许我需要一个有cookie意识的客户端。我可能需要修改客户端

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


这是链接:

会话状态从会话cookie运行,为了支持cookie,您需要向soap客户端添加cookie容器

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

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

您的
Web.config
中的会话配置是什么?会话在Web服务上非常有效。我认为是客户机没有保存cookies:(客户机代码是自动生成的。我无法修改它。它是一个web服务引用…感谢您的帮助!您在代码中实例化客户机时执行此操作。var client=new Test();CookieContainer cookieJar=new CookieContainer();client.CookieContainer=cookieJar;client.SetSession();bool isSessionSaved=client.isSessionSaved();即使我仍处于开发模式,我也要这样做吗?该代码没有在IIS上运行