【发布时间】: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.asax 的session_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