Quantcast
Channel: On All Things Web
Viewing all articles
Browse latest Browse all 17

A Modification to WURFL C# API for Device Detection

$
0
0

WURFL is a service used to detect mobile devices.  Stated on their web site, WURFL “is a Device Description Repository (DDR), i.e. a software component which contains the descriptions of thousands of mobile devices. In its simplest incarnation, WURFL is an XML configuration file plus a set of programming APIs to access the data in real-time environments.”

WURFL is used by various products, but can also be used directly in your web site.  If you are using .NET, whether web forms or MVC, you can get instructions on how to set it up here: http://wurfl.sourceforge.net/dotNet/

I had trouble getting the base installation to work correctly; I was getting a random error when it attempted to build the manager based on the configuration.  It was probably something I wasn’t doing,  so to quickly get support up and running, I chose to write my own wrapper around instantating of the manager.  Note that in this example, hard-coding the file strings was OK for me, since this was inside a web site and I was doing the same task over and over again.  If this was a reusable solution, passing these parameters in is preferred.

public class WurflDeviceManager
{
public const String WurflManagerCacheKey = “__WurflManager”;
public const String WurflDataFilePath = “~/App_Data/wurfl-latest.zip”;
public const String WurflPatchFilePath = “~/App_Data/web_browsers_patch.xml”;private static IWURFLManager CreateManager()
{
var wurflDataFile = HttpContext.Current.Server.MapPath(WurflDataFilePath);
var wurflPatchFile = HttpContext.Current.Server.MapPath(WurflPatchFilePath);
var configurer = new InMemoryConfigurer()
.MainFile(wurflDataFile)
.PatchFile(wurflPatchFile);var manager = WURFLManagerBuilder.Build(configurer);
HttpContext.Current.Cache[WurflManagerCacheKey] = manager;

return manager;
}

public static IWURFLManager GetManager(HttpContextBase context)
{
var wurflManager = context.Cache[WurflManagerCacheKey] as IWURFLManager;

if (wurflManager == null)
{
wurflManager = CreateManager();
}

return wurflManager;
}
}

The approach I took was to provide a lazy load, checking from cache first if the object exists, then instantiating it and storing in cache if it doesn’t.  This way, the instance is always guaranteed to be present.


Viewing all articles
Browse latest Browse all 17

Latest Images

Trending Articles



Latest Images