How To Store Information About The Sessions in Silverlight Applications For The Analysis – Part 2

Please begin by reading Part 1.

Now, create a table in the database and update the Entity Framework model. It may look like the image below:


SessionID – string, Max length 32, PK
Started – datetime
Ended – datetime, NULL allowed
IP – string, Max length 15
Country – string, Max length 32

Let’s create a helper class (SessionStatisticsService) to insert and update database records.

public void SessionStart(HttpContext Context)
{

try

{

SessionStatistics sessionStatistics = new SessionStatistics();

sessionStatistics.SessionID = Context.Session.SessionID;

sessionStatistics.Started = DateTime.Now;

sessionStatistics.Ended = null;

sessionStatistics.IP = IPToCountryService.GetIP(Context.Request.ServerVariables);

sessionStatistics.Country = “Unknown Country”;

string knownCountry;

if (IPToCountryService.GetCountry(sessionStatistics.IP, out knownCountry))

{

sessionStatistics.Country = knownCountry;

}

database.SessionStatistics.AddObject(sessionStatistics);

database.SaveChanges();

}

catch

{

// Do nothing here

}

}

public void SessionEnd(HttpSessionState Session)
{

SessionStatistics sessionStatistics = database.SessionStatistics.Where(x => x.SessionID == Session.SessionID).SingleOrDefault();

if (sessionStatistics != null)

{

sessionStatistics.Ended = DateTime.Now;

database.SaveChanges();

}

}

Will call the appropriate methods from a Global.asax.cs file.

protected void Session_Start(object sender, EventArgs e)
{

SessionStatisticsService sessionStatisticsService = new SessionStatisticsService();

sessionStatisticsService.SessionStart(Context);

}

protected void Session_End(object sender, EventArgs e)
{

SessionStatisticsService sessionStatisticsService = new SessionStatisticsService();

sessionStatisticsService.SessionEnd(Session);

}

To be continued…

Looking for quality Silverlight Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , | Posted under 3rd Party Software, ASP .NET 3.5, Programming/Coding | RSS 2.0

Leave a Reply

Your email address will not be published. Required fields are marked *


+ 9 = 10

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>