服务器变量没有从我的ASP页面到我的Silverlight应用程序

时间:2012-02-14 19:35:56

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

我有一个像这样的基本.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页面上添加的会话变量不再存在吗?

2 个答案:

答案 0 :(得分:1)

这个答案假设ASP classic首先进入方程式是有充分理由的。也许是因为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);

答案 1 :(得分:0)

您的asp.net会话变量不适用于Silverlight。他们只住在服务器上。

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