asp.net - 无法访问 wcf 服务中的 asp.net session

标签 asp.net wcf session jquery

我正在尝试访问 WCF 服务中的 asp.net session 。我正在从我的 asp.net 应用程序向 wcf 服务进行 jQuery AJAX 调用。我已经浏览了很多问题和文章,并尝试了他们所说的所有内容,但我仍然无法访问 wcf 服务中的客户端 session 。当我编写 DataSet ds = HttpContext.Current.Session["data"] as DataSet; 时,ds 始终为 null。

我在这里缺少什么?

这就是我的 WCF 服务的样子: //接口(interface)

[ServiceContract(SessionMode = SessionMode.Allowed)]
public interface IMyService
{
    [OperationContract(IsInitiating=true)]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    string GetData();

    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    bool SaveData(string data);
}

//服务

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class MyService : IMyService
{
    public string GetData()
    {
        return "something";

    }
    public bool SaveData(string data)
    {
        DataSet ds = HttpContext.Current.Session["data"] as DataSet;
        return true;
    }

}

我正在 Global.asaxsession_start 中创建一个数据集,并将其放入 session 中,以便只要用户 session 处于运行状态,我就可以在整个应用程序中使用它有效。

    protected void Session_Start(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();

        //Table to hold Product Selection
        DataTable dt = new DataTable("T1");
        dtSSNCertification.Columns.Add("Col1");
        ds.Tables.Add(dt);

        Session.Add("data", ds);
    }

这就是我的 wcf 项目的 web.config 绑定(bind) 的样子:

  <service name="MyService">
    <endpoint address="" behaviorConfiguration="JSONPBehaviorConfiguration"
      binding="customBinding" bindingConfiguration="jsonpBinding"
      contract="IMyService">
    </endpoint>
  </service>

  <customBinding>
    <binding name="jsonpBinding" >
      <jsonpMessageEncoding />
      <httpTransport manualAddressing="true"/>
    </binding>
  </customBinding>


  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
  </system.serviceModel>

最佳答案

要使用 ASP.NET 功能,您需要启用 ASP.NET 与 WCF 的兼容性。在您的 web.config 中设置:

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true">

关于asp.net - 无法访问 wcf 服务中的 asp.net session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11105764/

相关文章:

javascript - 从外部域调用 WCF 服务

asp.net - WCF 服务调用返回空文件/页面

php - 记住我的 cookie,需要 session cookie?

session - 心情 "Can not initialise PHP session, please verify that your browser accepts cookies."

c# - self 跟踪实体 SaveChanges() 在多对多关系中添加实体时出现异常

mysql - 即使表中有数据,数据也不是来自数据库,ORM 是 Dapper

c# - asp.net缓存多线程锁webparts

c# - string.Format 在 if 语句内时在 C# razor 中不起作用

java - 在没有 SSL 的情况下使用带有 Java 套接字的 session

asp.net - 使用 Jquery.post 时如何在 ASP.Net 中获取表单值