I am trying to access asp.net session in the WCF service. I am making jQuery AJAX
calls from my asp.net application to wcf service. I have gone thru many SO questions and articles and tried everything they said but still i am not able access the client session in wcf service. When i write DataSet ds = HttpContext.Current.Session["data"] as DataSet;
, ds
is always null.
What am i missing here?
This is how my WCF service looks like: //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);
}
//Service
[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;
}
}
I am creating a dataset in the session_start
of Global.asax
and putting it in the session so that i can use it across the app as long as user session is valid.
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);
}
This is how my wcf project's web.config bindings
looks like:
<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>
To use ASP.NET features you need to enable ASP.NET Compatibility with WCF. In your web.config set this:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.