【问题标题】:Why is session not saved in a simple hello world example web service为什么会话没有保存在一个简单的 hello world 示例 Web 服务中
【发布时间】:2014-04-24 03:09:31
【问题描述】:

我有以下网络服务:

[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

更新:如果我通过默认网站调用方法,我不认为问题出在网络服务上。我确定客户端没有保存 cookie。也许我需要一个 cookie 感知客户端。我可能需要修改客户端。

【问题讨论】:

  • 你的Web.config的会话配置是什么?
  • 会话在 Web 服务上效果很好。我认为是客户端没有保存 cookie :(

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


【解决方案1】:

会话状态脱离会话 cookie,为了支持 cookie,您需要将 cookie 容器添加到您的 soap 客户端。

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

在 WCF 服务客户端上,相当于在 httpbinding 配置上设置 allowCookies="true"。

【讨论】:

  • 客户端代码是自动生成的。我无法修改它。它是一个网络服务参考...感谢您的帮助!
  • 您在代码中实例化客户端时这样做。
  • var client = new Test(); CookieContainer cookieJar = new CookieContainer(); client.CookieContainer = cookieJar;客户端.SetSession(); bool isSessionSaved = client.IsSessionSaved();
【解决方案2】:

您必须在 IIS 上安装 Session 配置。

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

【讨论】:

  • 即使我仍处于开发模式,我也会这样做吗?该代码未在 IIS 上运行
猜你喜欢
  • 1970-01-01
  • 2017-06-23
  • 2013-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 2011-06-20
相关资源
最近更新 更多