In this article, I will teach you how to store information about the sessions in Silverlight Applications for analysis.
Why it is necessary?
Well, for example:
1. For the analysis of how many users are online at this moment;
2. For the analysis of geolocation by the IP addresses of users;
3. For the analysis of how many users visited the application today, yesterday, last 7 days and last 30 days;
We start with the implementation mechanism for maintaining ASP.NET sessions from the Silverlight application.
First, create a simple handler (KeepAliveHandler.ashx) that will update the session variable. It must implement an interface IRequiresSessionState:
public class KeepAliveHandler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Session[“Ping”] = DateTime.Now;
}
public bool IsReusable
{
get { return false; }
}
}
Add next to web.config:
<system.web>
<sessionState mode=”InProc” timeout=”15″ />
<httpHandlers>
<add verb=”GET,HEAD” path=”KeepAliveHandler.ashx” validate=”false” type=”XXX.KeepAliveHandler”/>
</httpHandlers>
</system.web>
Where XXX – namespace in which the handler, and “15” – the session timeout in minutes.
We call the handler from the App.xaml.cs file once every five minutes to maintain ASP.NET session.
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
RunKeepAliveService();
}
private void RunKeepAliveService()
{
var timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 5, 0);
timer.Tick += (s, args) =>
{
var client = new WebClient();
client.DownloadStringAsync(new Uri(“KeepAliveHandler.ashx”, UriKind.Relative));
};
timer.Start();
}
To be continued…
Looking for quality Silverlight Hosting? Look no further than Arvixe Web Hosting!