Wednesday, April 14, 2010

WCF [Global Variables] and [Caching]

WCF is stateless by nature and there is no support for global variables or caching out of the box. However, You can use Caching Application Block to create and access global variables that you might need in your WCF and also to improve performance. View this Power Point Presentation to get acquainted with Caching application block if you need to, otherwise read on.

In this article I am going to discuss how to use the Caching Application Block with WCF and I will show the details and the step by step instructions to add caching to your WCF. The diagram below is a block diagram of the pieces involved



This diagram shows a WCF service running in ASPNetCompatibilityMode. When the WCF starts it will load the cache proactively with look up data. when the look up data expire cache events will fire in order to load the new data. so let's begin.

Caching application block support two modes of caching those are "Proactive" and "Reactive".

In Proactive Caching data is loaded even before any of your WCF operations is called. This is particularly important for look up data because you want your look up data to be loaded and available to all the WCF operations at the start.

In Reactive Caching on the other hand, data is loaded into the cache via the WCF operations themselves.
To use caching application block and load data proactively you can follow the steps below.

1- Download the enterprise library from Microsoft website.
2- Create your WCF and make it support AspNetCompatibilityRequirements. (more on this here). To do so, decorate your WCF class (not the interface) with the following attribute
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

Then add this entry to your web.config inside the node system.serviceModel

3- Nowthat your WCF supports AspNetCompatibilityRequirements, then you can use global.asax. Add global.asax to your project and in the application_start load your cache.

4- Here is the code to load your cache
ICacheManager _wcfCache = CacheFactory.GetCacheManager();
_wcfCache.Add("table1", table1, CacheItemPriority.Normal, new RefreshTable1(), new ExtendedFormatTime("* * * * *"));

The first line will declare and load cache object and the second line will add an entry in the cache. This entry will be refreshed every minute using the ExtendedFormatTime expiration task
new ExtendedFormatTime("* * * * *")
and when refreshed the refresh method in a class called RefreshTable1 will be called to reload table1

The caching application block is also instrumented and that mean you can use PerfMon.exe to see how your cache is doing.


If you are new to performance counters watch this video to get your feet wet in custom counters and read this about performance counters. Here is also a sample application. The caching application block provides the following counters. Read More

Total Cache Misses - NumberOfItems64
Total Cache Hits - NumberOfItems64
Total Cache Expiries - NumberOfItems64
Total Cache Scavenged Items - NumberOfItems64
Total Cache Entries - NumberOfItems64
Total Updated Entries - NumberOfItems64
Total # of Cache Access Attempts - RawBase
Updated Entries/sec - RateOfCountsPerSecond32
Cache Scavenged Items/sec - RateOfCountsPerSecond32
Cache Expiries/sec - RateOfCountsPerSecond32
Cache Hits/sec - RateOfCountsPerSecond32
Cache Misses/sec - RateOfCountsPerSecond32
Cache Hits/sec - RateOfCountsPerSecond32
Cache Misses/sec - RateOfCountsPerSecond32
Cache Hit Ratio - RawFraction


In order to use PerfMon to see your run time cache performance you will need to use the server explorer inside your visual studio and add the keys above as displayed in the screen shots below.







After adding the instrumentation keys, you need to run PerfMon.exe and add the keys to the monitor as per the screen shots below







Then enjoy watching the performance of your cache as follows.







Wednesday, April 07, 2010

Fortify and Team Foundation Server

Do you have Team Foundation Server (TFS) and Fortify and wish they can work together automatically. This article will show one way of making fortify run every time you run a build on the Team Build server. After your build is completed a list of people will receive emails containing the fortify reports. Fortify reports will contain an fpr file that can be opened with the Audit work bench, an html file that can be opened with Internet explorer in addition to a log file.

Let's look at how we will do this.

Using Team Build we will override the "AfterComplie" target to add one Task this task will simply be an exec task. the exec task will run a batch file. This batch file will do all the fortify things. It will run fortify and email the files.

Let's see how we will do that in a step by step way.

Step 1:
-----------
Override the AfterComplie Target.


To do that, Check out your TFSBuild.Proj and just before the closing your Project element add the code highlighted in the screen shot above. in a nutshell what you need is an exec task as follows


The exec task will run fortify in from a batch file.

there are more than that in the screen shot above to use the ASPNetComplier task to combine your website dll's into one.


Step 2
---------------
Create runfortify.bat

Typically this file should look as follows

Rem 1. [CLEAN] Must clean first to clean C:\Documents and Settings\C649318\Local Settings\Application Data\Fortify\sca5.7\build
"E:\Program Files\Fortify Software\Fortify 360 v2.1.0\bin\sourceanalyzer" -b mybuild -clean



Rem 2. [TRANSLATE] Must translate second to create the intermediary Fortify Files. Must build solution first and use the Dll's folder of the solution. use -libdirs to reference any external dll's
"E:\Program Files\Fortify Software\Fortify 360 v2.1.0\bin\sourceanalyzer" -b mybuild -vsversion 8.0 -libdirs "C:\Documents and Settings\tfsservice\Local Settings\Temp\[ProjectName]\fortify\Sources\Main\Source\KPHC.Integration.WebUI\Bin" "C:\Documents and Settings\tfsservice\Local Settings\Temp\[ProjectName]\fortify\Sources\Main\Source\KPHC.Integration.WebUI\Bin" -debug -logfile "C:\Documents and Settings\tfsservice\Local Settings\Temp\[ProjectName]\fortify\Sources\Main\Source\fortifyTranslate.log"


REM [SCAN] and create an fpr and xml file in addition to logs
"E:\Program Files\Fortify Software\Fortify 360 v2.1.0\bin\sourceanalyzer" -b mybuild -scan -f "C:\Documents and Settings\tfsservice\Local Settings\Temp\[ProjectName]\fortify\Sources\Main\Source\FortifyIssues.fpr" -html-report -debug -logfile "C:\Documents and Settings\tfsservice\Local Settings\Temp\[ProjectName]\fortify\Sources\Main\Source\fortifyScan.log"

RunFortify.exe "C:\Documents and Settings\tfsservice\Local Settings\Temp\[ProjectName]\fortify\Sources\Main\Source\FortifyIssues.fpr" "C:\Documents and Settings\tfsservice\Local Settings\Temp\[ProjectName]\fortify\Sources\Main\Source\FortifyIssues.html" "C:\Documents and Settings\tfsservice\Local Settings\Temp\[ProjectName]\fortify\Sources\Main\Source\fortifyScan.log" "myemail@mydomain.com"



you will notice that the batch file also runs a program called RunFortify.exe this is a program that I created, all it does is to email the fpr, html and log files to a specific email address. I am not going to discuss this exe in this post. you can create your own exe that does that or use TFS to email the files.