1

I have a basic .ASP page like this, which when hit redirects the user to my SL app after adding a value to the session variables

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

When I get to my SL app hosted on myapp.aspx, the first think it does it check for the lsv QueryString. If it equals true, it calls a WCF service with code like

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

Does anyone know why the session variable that I just added on the ASP page before the redirect no longer exists when my SL app tried to fetch it?

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
Josh
  • 1,648
  • 8
  • 27
  • 58

2 Answers2

1

This answer assumes there is a good reason why ASP classic enters into the equation in the first place. Perhaps its because Silverlight is being introduced into an existing ASP site. The problem is that most Silverlight Client-Server examples involve .NET WCF on the server.

The answer to your problem is don't use a WCF service to fetch your session data. Use a simple ASP page instead. It should be fairly straight-forward to use a simple XML structure to carry the session data you want to the Silverlight app. Use a DTO class that can be used to deserialise the XML into a simple class. Something like this:

(Caveat: air code)

 <%
     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
 %>

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

 }

Now in some UI or ViewModel you do

     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);
AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
0

Your asp.net session variables are not available to Silverlight. They live in the server only.

Check this simple workaround. It might help you.

NestorArturo
  • 2,476
  • 1
  • 18
  • 21
  • That's why I'm using a WCF service to get the session variables... If I go from default.asp to myapp.aspx, once on myapp.aspx there is no longer a Session. Why is this? – Josh Feb 14 '12 at 20:00
  • My mistake. As SLaks states, ASP != ASP.Net. That's why you need other means to pass information between .asp and .aspx as they are basically incompatible technologies. For example, you can use the query string. Even you say you are reading the Query String, you are not: you are trying to read the Session collection. For reading a query string use something like Page.Request.QueryString["lsv"] – NestorArturo Feb 14 '12 at 20:11
  • You are confused. I am using both query strings and session data. The query string value tells my app to call the wcf service to pull the session data. The problem is that the session data is not there, even though I clearly put it there right before the redirect. – Josh Feb 14 '12 at 20:18
  • Ok... you are reading the querystring elsewhere. Still ASP != ASP.Net, this is why they do not share the same Session collection ¿got it?. An easy fix could be to turn your ASP page to ASPX so they can share almost everything. – NestorArturo Feb 14 '12 at 20:41