gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:02:46 24 4
gpt4 key购买 nike

我正在尝试访问 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com