【问题标题】:unable access asp.net session in wcf service无法访问 wcf 服务中的 asp.net 会话
【发布时间】:2012-06-21 18:36:44
【问题描述】:

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

我在这里错过了什么?

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

[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 中创建一个数据集并将其放入会话中,这样只要用户会话有效,我就可以在整个应用程序中使用它。

    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 bindings 的样子:

  <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 wcf session jquery


    【解决方案1】:

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

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

    【讨论】:

    • 它不工作是因为我正在对我的服务进行 AJAX 调用吗?
    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 2011-05-31
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多