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?
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);