【问题标题】:Server Variables not making it from my ASP page to my Silverlight App服务器变量没有从我的 ASP 页面到我的 Silverlight 应用程序
【发布时间】:2012-02-15 05:32:04
【问题描述】:

我有一个像这样的基本 .ASP 页面,当点击该页面时,会在向会话变量添加值后将用户重定向到我的 SL 应用程序

<!-- Default.htm -->
<html>
<%Session("valuekey")=somevalue%>
<Head>
<META http-equiv="Refresh" content="0; URL=/appdirectory/myapp.aspx?lsv=true"></HEAD>
<Body></Body>
</HTML>

当我访问托管在 myapp.aspx 上的 SL 应用程序时,首先认为它会检查 lsv QueryString。如果它等于 true,它会调用一个 WCF 服务,代码如下:

object x = HttpContext.Current.Session["valuekey"];
if(x == null)
{
ServiceError.Message = "No session variable found";
}
else
{
return x.ToString();
}

有谁知道为什么当我的 SL 应用程序尝试获取它时,我刚刚在重定向之前添加到 ASP 页面上的会话变量不再存在?

【问题讨论】:

  • ASP 会话 != ASP.Net 会话

标签: silverlight silverlight-4.0 asp-classic wcf-ria-services session-variables


【解决方案1】:

此答案假定 ASP 经典首先进入等式是有充分理由的。可能是因为 Silverlight 被引入现有的 ASP 站点。问题是大多数 Silverlight 客户端-服务器示例都涉及服务器上的 .NET WCF。

您的问题的答案是不要使用 WCF 服务来获取您的会话数据。请改用简单的 ASP 页。使用简单的 XML 结构将您想要的会话数据传送到 Silverlight 应用程序应该是相当直接的。使用可用于将 XML 反序列化为简单类的 DTO 类。像这样的:

(警告:航空代码)

 <%
     Dim dom:  Set dom = CreateObject("MSXML2.DOMDocument.3.0")
     dom.loadXML "<SessionData />" 

     AddElem dom.documentElement, "ValueKey", Session("valuekey")
     AddElem dom.documentElement, "SomeOtherValue", Session("othervalue")
     ''# include other session values needed by client here.

     Response.ContentType = "text/xml"
     Response.CharSet = "utf-8"
     dom.save Response

     Sub AddElem(parent, name, value)
         Dim elem: Set elem = parent.ownerDocument.createElement(name)
         parent.appendChild elem
         elem.text = value;
     End Sub
 %>

在 Silverlight 中:

 [DataContract]
 public class SessionData
 {
     [DataMember(Order=1)]
     public string ValueKey {get; set; }

     [DataMember(Order=2)]
     public string SomeOtherValue {get; set; }

     public static void Fetch(Action<string> returnResult, Action<exception> fail)
     {
         WebClient client = new WebClient();
         OpenReadCompletedEventHandler eh = null;
         eh = (s, args) =>
         {
             try
             {
                 var sr = new DataControlSerializer(typeof(SessionData));
                 returnResult((SessionData)sr.ReadObject(args.Result)); 
             }
             catch (Exception e)
             {
                 fail(e);
             }
             finally
             {
                client.OpenReadAsyncCompleted -= eh;
             }
          };
          client.OpenReadAsyncCompleted += eh;
          client.OpenReadAsync(new Uri("../serviceFolder/sessionState.asp", UriKind.Relative));
     }

 }

现在你可以在一些 UI 或 ViewModel 中使用

     void SessionData_Available(SessionData sessionData)
     {
          _sessionData = sessionData;
          // Other actions needed once session data has arrived.
     }

     void ReportProblem(Exception e)
     {
          // Some UI change to inform user of failed fetch
     }

...

     SessionData.Fetch(SessionData_Available, ReportProblem);

【讨论】:

    【解决方案2】:

    您的 asp.net 会话变量对 Silverlight 不可用。它们只存在于服务器中。

    Check this simple workaround。它可能会对你有所帮助。

    【讨论】:

    • 这就是为什么我使用 WCF 服务来获取会话变量...如果我从 default.asp 转到 myapp.aspx,一旦在 myapp.aspx 上不再有会话。这是为什么呢?
    • 我的错误。正如 SLaks 所说,ASP != ASP.Net。这就是为什么您需要其他方式在 .asp 和 .aspx 之间传递信息的原因,因为它们基本上是不兼容的技术。例如,您可以使用查询字符串。即使您说您正在阅读查询字符串,但您不是:您正在尝试阅读 Session 集合。要读取查询字符串,请使用 Page.Request.QueryString["lsv"]
    • 你很困惑。我同时使用查询字符串和会话数据。查询字符串值告诉我的应用程序调用 wcf 服务来提取会话数据。问题是会话数据不存在,即使我在重定向之前明确将其放在那里。
    • 好的...您正在其他地方阅读查询字符串。仍然是 ASP != ASP.Net,这就是为什么它们不共享相同的 Session 集合 ¿ 明白了吗?一个简单的解决方法是将您的 ASP 页面转换为 ASPX,以便他们可以共享几乎所有内容。
    猜你喜欢
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    相关资源
    最近更新 更多