【问题标题】:share session Asp.NET webservice - silverlight共享会话 Asp.NET 网络服务 - silverlight
【发布时间】:2011-06-16 13:58:19
【问题描述】:

我有一个看起来像这样的网络服务

[WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Connector : System.Web.Services.WebService
    {
        private static readonly ILog log = MLogger.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);


        [WebMethod(EnableSession = true)]
        public void SetSession(string session, string value)
        {
            Session[session] = value;
        }

        [WebMethod(EnableSession = true)]
        public string GetSession(string session)
        {
            string result = HttpContext.Current.Session[session] != null ? (string)HttpContext.Current.Session[session] : null;
            log.Info("GetSession :" + session +"-"+result);
            return result;
        }

    }

我有一个包含 Silverlight 应用程序的 asp.net 页面。 该页面设置了一个 Session 密钥:

 if (Session["Token"] == null)
                {
                    Session["Token"] = Token.GetToken(Connection, HttpContext.Current.Request.ServerVariables["HTTP_HOST"], "")[0].Value;
                    log.Info("Token:" + Session["Token"].ToString());
                }

问题是,当我从 silverlight 应用程序调用 web 服务以获取“令牌”会话值时,它为空.. 日志如下所示:

Token:e4d46740-2bb1-4956-a27b-a1af0b908acc <-this is where the session is set
GetSession :Token- <-this is in the webservice where GetSession("Token") is called...

我做错了什么?如何与 web 服务共享会话数据? 谢谢, 波格丹

【问题讨论】:

    标签: asp.net web-services silverlight-4.0


    【解决方案1】:

    您用于存储数据的Key是字符串“Token”,它是一个常量,而要检索它,您肯定会使用其他字符串作为Key强>.

    【讨论】:

    • 当我检索它时,我在日志中键入密钥: log.Info("GetSession :" + session +"-"+result);在日志文件中我有 GetSession :Token-
    • 从头开始制作一个小项目后,我可以从服务中获得我在 aspx 页面中设置的任何值。尝试在一个干净的新项目中测试您的代码。如果您使用的是 WCF,请注意这种服务可以配置为在每个请求上创建一个新的“会话”,这样会丢失对会话所做的任何更改 ¿ 这可能是您的情况吗?也可以尝试没有 WebServiceBinding 属性的服务。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 2010-11-28
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多