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.







25 comments:

Anonymous said...

Hi,
I have read your article and implemented the code. But I cannot manage to call CacheFactory.GetCacheManager(); without getting an:

System.Threading.SynchronizationLockException occurred Message=Object synchronization method was called from an unsynchronized block of code. Source=Microsoft.Practices.Unity StackTrace: at Microsoft.Practices.Unity.SynchronizedLifetimeManager.TryExit() in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\Lifetime\SynchronizedLifetimeManager.cs:line 109 InnerException:

Any clues what is going on?

BR
//Chris

Anonymous said...

Get article. Thanks for the post! This helped me to solve a problem that I searched for online for a while.

Anonymous said...

Every weekend i used to go to see this web site,
because i wish for enjoyment, since this this web site conations genuinely nice funny stuff too.


Here is my website ... calories walking

Anonymous said...

Wow, marvelous blog layout! How long have you been
blogging for? you make blogging look easy. The overall look of your website is excellent,
as well as the content!

My weblog registry cleaner

Anonymous said...

whoah this weblog is excellent i really like studying your posts.
Keep up the great work! You realize, a lot of people are searching around for this info, you could
help them greatly.

My blog :: reformes ()

Anonymous said...

I am sure this paragraph has touched all the internet visitors,
its really really pleasant paragraph on building up new web site.


My blog post :: payday loans

Anonymous said...

Very good article. I certainly appreciate this website.
Keep it up!

My weblog :: Buy AndroX

Anonymous said...

My spouse and I stumbled over here coming from a different web address and thought I might check things
out. I like what I see so now i'm following you.
Look forward to finding out about your web page again.

my blog post; senuke xrumer service

Anonymous said...

Wonderful blog! I found it while browsing on Yahoo News. Do you have any suggestions on how to get listed
in Yahoo News? I've been trying for a while but I never seem to get
there! Thank you

Feel free to visit my web blog :: Best Magic Submitter Review

Anonymous said...

Hi there, the whole thing is going perfectly here and ofcourse
every one is sharing facts, that's truly fine, keep up writing.


my webpage: Personals Ads Virginia

Anonymous said...

Normally I do not read post on blogs, however I would like to say that this write-up very pressured me to check out and do so!

Your writing taste has been surprised me. Thank you, quite
great post.

my homepage family guy the quest for stuff hack

Anonymous said...

Hi there, I found your blog by way of Google whilst searching
for a comparable matter, your web site came up, it seems good.
I have bookmarked it in my google bookmarks.
Hello there, simply became aware of your blog through Google, and located that it is truly informative.
I am going to watch out for brussels. I'll be grateful when you
proceed this in future. A lot of folks shall be benefited out of your writing.
Cheers!

Take a look at my blog post :: goji berry

Anonymous said...

Hi there, just wanted to tell you, I liked this post. It was inspiring.
Keep on posting!

my blog :: Tim Mcgraw Concert Reviews

Anonymous said...

Very nice post. I just stumbled upon your weblog and wished to say
that I've really enjoyed surfing around your blog posts.
In any case I will be subscribing to your feed and I hope you write again soon!

Check out my blog post :: air compressor for electronics

Anonymous said...

Hi there are using Wordpress for your site platform?
I'm new to the blog world but I'm trying to get started and create my own. Do you require any coding knowledge to make your
own blog? Any help would be really appreciated!

my webpage get your ex back text examples

Anonymous said...

Generally I don't learn post on blogs, however I would like to say that
this write-up very pressured me to take a look at and do so!
Your writing taste has been amazed me. Thank you, very
great post.

My weblog attorney reviews Cherry Hill NJ

Anonymous said...

The goal of the games on your cell phone is a great variety
of free mobile games, Play Online Flash Games. The
screen also shows you the call, and then you definitely tend to be cheaper and usually cost
in the $0. Also, while the next day you might be in Boston, battle nations hack while the usual interface options are available, on-screen joysticks aren't always
my favorite but it works well for the company.
It also has puzzles for elders, say, and then the faster
time flies, you make you relax, physically and mentally.



My site: Battle nations hack bot

Anonymous said...

Hello colleagues, its impressive paragraph on the topic
of tutoringand entirely explained, keep it up all the time.



Feel free to visit my site; medical negligence solicitors

Anonymous said...

Awesome! Its really amazing post, I have got much clear idea regarding from this piece of writing.


Here is my blog: mltek forum

Anonymous said...

what you see them for a lot of grouping comprehend they economise a ton of
web practical application wish relinquish your plants to a marriage
ceremony, it is the difficulty with an aromatherapy treatment.
Along with a project pass or else. merely roll it
some the audience? Why not lie on a Terrell Suggs Jersey Michael Bennett Youth Jersey Bradie Ewing Authentic Jersey
Jason McCourty Jersey Dominic Raiola Jersey
Matt Barkley Authentic Jersey Donnie Jones Jersey Jeff Linkenbach Jersey Walt Aikens Womens Jersey Cody Latimer Authentic Jersey Dwayne Allen Authentic Jersey Bryan Stork Jersey Jerome Simpson Womens Jersey Jack Mewhort Jersey Matt Giordano Jersey Will Hill Youth Jersey Danny Trevathan Jersey
James Hanna Jersey william hayes jersey Corey Linsley Jersey Eric Fisher Jersey Dwight Lowery Youth Jersey Barkevious Mingo Authentic Jersey
Alshon Jeffery Womens Jersey Jim McMahon Youth Jersey Courtney Upshaw Youth Jersey be far
less costly than the ones joint Greek deity can back
up lead you to mount just about extraordinary visual communication commercialism is to just provoke up on your bod.
This is distinguished as each anatomy is involved in all forms and only
focuses on documenting the live held by its employees,

Also visit my weblog: Cordarrelle Patterson Jersey

MaĆ­ra said...

I loved the text, they are to be congratulated !!

yanmaneee said...

yeezys
kobe 11
nike air max
nike air max
supreme hoodie
bape hoodie
nhl jerseys
cheap jordans
jordan 11
goyard handbags

Diamondexch said...

In 2021, the world777 online gaming audience in India grew 28 per cent to reach Rs 101 billion or crore ( roughlyUS$1.3 billion), according to a report published in March 2022 by EY, a global professional services company, and the Federation of Indian Chambers of Commerce & Industry (FICCI).

Anonymous said...

OFF-White
yeezy
jordan 1 off white
jordan 6
kyrie irving shoes
supreme clothing
golden goose outlet
bape
alexander mcqueen outlet
supreme clothing

Anonymous said...

jordan shoes
goyard bag
off white outlet
kd 12
golden goose
golden goose clearance
supreme
hermes outlet
kobe shoes
supreme new york