Reputation: 87
I have a WCF service. In global.asax
, I put some data into the ASP.NET session. But when I call the WCF method, the Session
object is always NULL
.
This is my WCF service
[WebMethod(EnableSession = true)]
public List<Menu> GetMenus()
{
List<Menu> menulist = new List<Menu>();
Object[] O = HttpContext.Current.Session["menu"] as Object[];
foreach (var item in O)
{
Menu menu = new Menu();
menu.Html = ((WcfServices.Menu)(item.ToType(typeof(Menu)))).Html;
menu.Label = ((WcfServices.Menu)(item.ToType(typeof(Menu)))).Label;
menulist.Add(menu);
}
return menulist;
}
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
List<Menu> GetMenus();
Here is my global.asax
file
protected void Session_Start(object sender, EventArgs e)
{
CrmHelper helper = new CrmHelper();
if (Session["service"] == null)
{
IOrganizationService s = helper.CreateService(true);
Session["service"] = s;
}
MobileHelper mobilehelper = new MobileHelper((IOrganizationService)Session["service"]);
if (Session["menu"] == null)
{
Session["menu"] = mobilehelper.GetMainMenus();
}
}
Accessing Session["service"]
or Session["menu"]
always returns null
when I call the WCF service.
Any ideas?
Upvotes: 0
Views: 763
Reputation: 754488
The main issue is: WCF is NOT ASP.NET !
By default, WCF does NOT rely on the ASP.NET runtime (and that's a Good Thing!!) and thus doesn't have access to the ASP.NET constructs like Session
...
If you need to provide some data to a WCF service, best place to put that info is a database table where the WCF service can load the information from.
If you insist on using ASP.NET session storage, and you don't mind that you're limiting yourself to using only IIS with ASP.NET as the hosting environment for your WCF service in that case, check out this blog post showing exactly what you need to do to gain access to the ASP.NET session state.
Upvotes: 1