WCF服务会话始终为NULL

时间:2013-09-04 16:39:55

标签: wcf

我有一个WCF服务。在global.asax中,我将一些数据放入ASP.NET会话中。但是当我调用WCF方法时,Session对象始终是NULL

这是我的WCF服务

[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();

这是我的global.asax文件

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();
     }
}

当我调用WCF服务时,访问Session["service"]Session["menu"]始终会返回null

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

主要问题是:WCF ASP.NET!

默认情况下,WCF NOT 依赖于ASP.NET运行时(这是 Good Thing !!),因此无法访问ASP。 NET构造如Session ...

如果您需要向WCF服务提供一些数据,那么放置该信息的最佳位置是WCF服务可以从中加载信息的数据库表。

如果您坚持使用ASP.NET会话存储,并且您不介意将自己限制为仅使用 IIS和ASP.NET作为在这种情况下,为您的WCF服务托管环境,check out this blog post显示您需要做什么才能访问ASP.NET会话状态。