Archive

Posts Tagged ‘Visual Studio 2010’

Using MemoryCache in .Net 4.0

November 27, 2011 2 comments

Since ASP.Net first came, it came up with a very powerful feature of in-memory object cache (System.Web.Caching.Cache) to store commonly used expensive data on server side. Almost every ASP.Net application/site uses this feature now. But it suffered from few shortcomings like-

  • It is available in ASP.Net only leaving WinForms or WPF clients puzzled.
  • It is not extensible to accommodate other demands to store cache objects in disk or Sql Server. However, MS after realizing these shortcomings, Caching Application Block library was included in its Enterprise Library.
  • .Net developers have to look for their own mechanisms to create logical in-memory partitions called regions to group or organize cache objects in memory.

However, .Net 4.0 came up with a new set of caching APIs in System.Runtime.Caching namespace that addresses all the above shortcomings. This new namespace can be found in System.Runtime.Caching.dll assembly. But this assembly reference is available only target framework of .Net Framework 4, not .Net Framework 4 Client Profile.

The System.Runtime.Caching namespace contains two core set of classes:

  • Concrete implementation of System.Runtime.Caching.MemoryCache class to support in-memory object cache. MemoryCache is closely modeled after old System.Web.Caching.Cache of ASP.Net. However, one does not have to rely upon System.Web assembly to use MemoryCache (We will see it through an example shortly).
  • Abstract types to build custom cache implementation other than in-built MemoryCache.

Now let’s move to try out an example to leverage MemoryCache features. First of all we will create a class library type project. Add a class MyCache and assembly reference of System.Runtime.caching.dll.

MyCache Code sample:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Caching;

namespace DotNetCachingWrapper
{
public enum MyCachePriority
{
Default,
NotRemovable
}

public class MyCache
{
// Gets a reference to the default MemoryCache instance.
private static ObjectCache cache = MemoryCache.Default;
private CacheItemPolicy policy = null;
private CacheEntryRemovedCallback callback = null;

public void AddToMyCache(String CacheKeyName, Object CacheItem, MyCachePriority MyCacheItemPriority, List<String> FilePath)
{
//
callback = new CacheEntryRemovedCallback(this.MyCachedItemRemovedCallback);
policy = new CacheItemPolicy();
policy.Priority = (MyCacheItemPriority == MyCachePriority.Default) ? CacheItemPriority.Default : CacheItemPriority.NotRemovable;
policy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(10.00);
policy.RemovedCallback = callback;
policy.ChangeMonitors.Add(new HostFileChangeMonitor(FilePath));

// Add inside cache
cache.Set(CacheKeyName, CacheItem, policy);
}

public Object GetMyCachedItem(String CacheKeyName)
{
//
return cache[CacheKeyName] as Object;
}

public void RemoveMyCachedItem(String CacheKeyName)
{
//
if (cache.Contains(CacheKeyName))
{
cache.Remove(CacheKeyName);
}
}

private void MyCachedItemRemovedCallback(CacheEntryRemovedArguments arguments)
{
// Log these values from arguments list
String strLog = String.Concat("Reason: ", arguments.RemovedReason.ToString(), "
| Key-Name: ", arguments.CacheItem.Key, " | Value-Object: ",
arguments.CacheItem.Value.ToString());
}

}
}

Build this class library to generate assembly name of MyCachingWrapper.dll. Now we are ready to use this library in all ASP.Net web, WinForms and WPF applications.

Now let’s see examples of how this library can be commonly used in both ASP.Net Web Form and WinForm after adding reference of MyCachingWrapper.dll.

ASP.Net Page_Load:

protected void Page_Load(object sender, EventArgs e)
{
//
MyCache objCache = new MyCache();
String strUserName = objCache.GetMyCachedItem("USER_NAME") as String;
if (String.IsNullOrEmpty(strUserName))
{
List<String> lstFiles = new List<string>();
lstFiles.Add(HttpContext.Current.Request.MapPath("~/XmlFiles/ListOfUsers.xml"));

XElement x = XElement.Load(HttpContext.Current.Request.MapPath("~/XmlFiles/ListOfUsers.xml"));
var qry = from u in x.Elements("Users") where u.Element("UserCode").Value == "101" select u;
strUserName = qry.First().Element("UserName").Value;

// Add inside cache
objCache.AddToMyCache("USER_NAME", strUserName, MyCachePriority.Default, lstFiles);
}
this.lblUserName.Text = strUserName;
}

WinForm Form_Load:

private void frmDefault_Load(object sender, EventArgs e)
{
//
MyCache objCache = new MyCache();
String strFilePath =System.IO.Path.Combine(Environment.CurrentDirectory, "../../XmlFiles/ListOfUsers.xml");
String strUserName = objCache.GetMyCachedItem("USER_NAME") as String;
if (String.IsNullOrEmpty(strUserName))
{
List<String> lstFiles = new List<string>();
lstFiles.Add(strFilePath);

XElement x = XElement.Load(strFilePath);
var qry = from u in x.Elements("Users") where u.Element("UserCode").Value == "101" select u;
strUserName = qry.First().Element("UserName").Value;

// Add inside cache
objCache.AddToMyCache("USER_NAME", strUserName, MyCachePriority.Default, lstFiles);
}
this.lblUserName.Text = strUserName;
}

Sample ListOfUsers.xml File:

<?xml version="1.0"?>
<ListOfUsers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
<Users>
<UserCode>101</UserCode>
<UserName>Dan Brown</UserName>
</Users>
<Users>
<UserCode>102</UserCode>
<UserName>da Vinci</UserName>
</Users>
<Users>
<UserCode>103</UserCode>
<UserName>Monalisa</UserName>
</Users>
<Users>
<UserCode>104</UserCode>
<UserName>Shakespeare</UserName>
</Users>
<Users>
<UserCode>105</UserCode>
<UserName>William Wordsworth</UserName>
</Users>
</ListOfUsers>

Now we can debug above code and see other interesting things. You will see the usage of MemoryCache methods and its techniques to cache objects are similar to what we have been doing in ASP.Net cache so far. Also, we see how the same MyCache library is usable in both web and windows applications.

MemoryCache points worth mentioning:

  • MemoryCache.Default returns the single and same instance of in-memory ObjectCache through a static read-only property.

    public static MemoryCache Default { get; }

  • CacheEntryRemovedCallback signature is different than what we have seen in prior versions of ASP.Net. Refer to example. In the current callback method, one can see the following details when cache item is expired after 10 seconds.

CallbackArgumentsDetails

  • CacheItemPriority enum in .Net 4.0 is cleaner than what we have seen in prior versions of ASP.Net. Now, it is only Default and NotRemovable.
  • In prior version of ASP.Net, CacheDependency was used to monitor changes in any underlying objects like files, Sql database tables, rows, columns, etc. Now .Net 4.0 provides ChangeMonitor class which is ASP.Net neutral and has wider scope of monitoring dependent objects to expire cache items. However, CacheDependency is still there in ASP.Net 4.0. We should use other implementation of ChangeMonitor like HostFileChangeMontor in the above example.
  • The default implementation of MemoryCache does not give us flexibility to add regions along with keys. In order to use regions, you should extend MemoryCache.

Hope you all will appreciate .Net 4.0 caching feature.

Advertisements

Embedding and Using Resources from .Net Assembly

.Net Assemblies can contain various types of resources like images, icons, files, etc. Such resources are mostly static, i.e.; do not keep changing during run time or application wise. Also, such resources are not executable items. So while deploying such assemblies, we need to make sure those resources are intact with the packaged assemblies. Else, assemblies may blow up while executing the resource dependent methods. So as part of deployment strategy, we should embed such resources into the assembly itself.

Today we will see one such example of embedding Xml file into assembly.

  • Add one Class Library type project into the solution.
  • Add one Xml file as well. Fill Xml file with few data that the class library may use to query.
  • Right click on the Xml file, and select Properties –> Build Action. Out of several Build Action options, select Embedded Resource.

ResourceProperties

  • There you may see other properties as well like Copy to Output Directory. It has options like this:

ResourceSettings

  • If you select Do Not Copy, output of building the class library inside \bin\ folder will not have separate *.dll than embedded Xml file.
  • If you select Copy Always, output of building the class library inside \bin\ folder will always have *.dll and an embedded Xml file.

However, I prefer the first option. By this we make sure our distributable assembly is only one *.dll. This way the embedded resource Xml file cannot be modified, and our assembly can safely execute methods that depend upon this Xml file. I like to suggest one more tips here- rename this Xml file extension to *.config. Renaming Xml file extension to *.config makes file not browse able by Browser in web applications.

Here I have shown steps of embedding resource file into .Net assembly. This assembly can be either Class Library or Web application. We cannot embed resource file into web site type project as Web site does not produce assembly like Web application.

Now, let’s see how we can access embedded Xml file from assembly during runtime.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Xml;
using System.IO;

namespace MathLibrary
{
public class MathLibrary
{
//
private const string strFileName = "XMLFile.config";

public System.Xml.XmlDocument GetXMLDocument()
{
//
var assembly = Assembly.GetExecutingAssembly();
var stream = assembly.GetManifestResourceStreamthis.GetType(), strFileName);
var doc = new XmlDocument();

try
{
if (stream == null)
{
throw new FileNotFoundException("Couldnot find embedded mappings resource file.", strFileName);
}
doc.Load(stream);
}
catch (Exception ex)
{ throw ex; }
return doc;
}

public System.IO.Stream GetXMLStream()
{
//
var assembly = Assembly.GetExecutingAssembly();
var stream = assembly.GetManifestResourceStream(this.GetType(), strFileName);

try
{
if (stream == null)
{
throw new FileNotFoundException("Couldnot find embedded mappings resource file.", strFileName);
}
}
catch (Exception ex)
{ throw ex; }
return stream;
}
}
}

There are two methods shown above- one returning XmlDocument object and other returning IO.Stream. Either of the return type can be used to construct XmlDocument object now at caller end of these methods.

Now let’s verify the assembly if it has any embedded resource or not. We can do this using either Reflector or Ildasm tool.

Reflector View:

ReflectorView

Ildasm View:

IldasmView

See resource name is qualified with assembly namespace name prefixed to it- MathLibrary.XMLFile.config.

Happy Coding!

Visual Studio 2010: Exporting and Importing Break-Points

Coding less and debugging more has always been part of programming. Debugging of code is always accompanied by locating appropriate break-points and inspecting program execution. But locating break points again and again whenever a solution is opened for debugging consumes time. When we feel the program execution is happening as expected, then we either delete break-points or disable them. Overall, programmers spend few minutes for break-points activities.

Visual Studio 2010 comes with new feature of exporting and importing break-points in an xml file. So when we are done with break-points, we can export and save at a physical location. Then, delete the break-points (Ctrl + Shift + F9). Whenever we require break-points again in code, we can import the settings file that we saved last time. This xml file contains all the required settings of break-points in the code like line number, file name, etc.

Snapshots shown below are self-explanatory for .Net programmers.

Open break-point window (Ctrl + Alt + B).

ExportBreakPoints
Fig 1: Exporting break-points

ImportBreakPoints

Fig 2: Importing break-points

After importing break-point xml file, the break-points are again set at lines and files where break-points were set originally.

BreakPoint-XmlStructure
Fig 3: Break-point xml structure

Cheers!