Welcome to MSDN Blogs Sign in | Join | Help

I just got word via the Software Association of New Hampshire (SwANH) about a series of five workshops being run by WEST, a non-profit organization focused on women in science and technology.  Unfortunately, three have already been held, but the other two are next Monday and the following.  Here’s are the topics (copied from their registration site):

CORPORATE SAVVY

Monday July 13, 2009
5:30pm-9:00pm Cambridge, MA

A three-hour interactive, participatory workshop in which you will learn how to navigate corporate cultures in order to succeed. Workshop leader is Sandra Glucksmann PhD, Senior VP of Research and Business Operations at Cerulean Pharmaceuticals.
MORE INFO & REGISTRATION

MENTORING: HOW TO FIND (AND BE) A GREAT MENTOR

Monday July 20, 2009
5:30pm-9:00pm Cambridge, MA

This workshop addresses the fundamentals of good mentoring relationships, from both the perspective of the mentor, and the mentee. Learn how to find mentors, and how to be a great mentor yourself.  Workshop leader is Joanne Kamens PhD who is Director of Discovery at RXi Pharmaceuticals, frequent speaker on this topic, and has implemented successful mentoring programs. 
MORE INFO & REGISTRATION

These events have a charge associated with them and are being held in Cambridge at 1 Main Street, which should be right across the street from the Microsoft NERD Center.

Thrive for Developers

On Monday I posted on some ways to stay informed of .NET happenings within your area.  Just today, Microsoft launched a new program, Thrive for Developers, tailored specifically on the developer community in these times of economic uncertainty.

Thrive for Developers is focused on providing you the resources to advance your career to the next level, to enhance your skills, or just to connect and network with the Microsoft development community.   Check it out and share the links with your developer friends – there’s even a ThriveDev Job Site for those of you who are (or know someone who is) looking for employment.

Win $10000I missed the mark on this one, but thanks to a recent posting from Dino Esposito, I just became aware of ComponentArt’s 2009 Summer Silverlight Coding Competition

The best application (as selected by the panel and community votes) will win $10,000, and two-runners up will receive ComponentArt licenses.  Use of ComponentArt products is NOT required.  Applications will start appearing for community voting today, but you have through September 22 to submit your entries.

Happy New Year!

Yes, you’re still recovering from Fourth of July festivities, and, no, I didn’t get too much sun over the long weekend.   July 1st indeed starts a new year, a new fiscal year, at Microsoft, and with that comes some intense planning for the coming 12 months – budgets, sales quotas, annual reviews, etc.  For us, the Developer Platform and Evangelism team (DPE), specifically it’s a time for planning out our various events and road shows as well as fostering community activities like code camps and ‘firestarters.’

So what’s the best way to keep on top of all these happenings during the upcoming year?

  • MSDN Flashsubscribe here and enter your ZIP code to get localized content from Chris Bowen and myself in the Northeast.  Our colleagues in other regions likewise add their own local flair to the bi-weekly newsletter.

     

  • Community Megaphone – on my blog’s sidebar is a feed showing the events in the Northeast district, but you can visit the main site and see what’s going on around the country.

     

  • Follow your evangelists’ blogs.  Well, you found mine and the rest of the members of the East region developer evangelist family are listed in my blog’s sidebar.

     

  • Get involved in a User Group.  In the Northeast, we have nearly three-dozen active groups focused on various aspects of Microsoft technologies, and there’s many more independent groups focused on complementary topics like JavaScript, Web technologies, and user experience as well.  There’s no better way to hear about activities than being part of those activities yourself.  It’s a double-win: you stay abreast of technology, and you meet some great people in the process.  

     

ZAAs you might be aware, this week ended my A-Z series exploring various facets of Microsoft and .NET technologies.  It was actually a lot of fun to explore a different topic each week, and I’m even happier to say I didn’t miss one Monday, making me something like 1 for10 in terms of fulfilling New Year’s resolutions.  With 26 letters in the alphabet leading to 26 posts, that also means 2009 is half-over, yikes!

Just to pull it all together, here’s a complete listing of the posts in this series.  I hope you got something out of one or more of the posts, and I’m always looking for new ideas, so let me know if there’s something you’d like to hear about on a more or less regular basis.

 

.NET from A-Z (introductory post)

A is for Azure B is for BAML C is for Color
D is for Dependency Property E is for Extension Method F is for F#
G is for Generation H is for HierarchyID I is for Isolated Storage
J is for JSON K is for Key L is for Lambda
M is for M N is for Nullable<T> O is for Oomph
P is for Partial Method Q is for Quirks R is for REST
S is for SQLMetal T is for Tracepoint U is for Unchecked
V is for Velocity W is for WeakReference X is for XML Literal
Y is for Yield Z is for Zermatt  

Z

Zermatt is, well actually was, the code name for Microsoft’s next generation identity and access management API.   Zermatt is now known by its new code name of “Geneva” Framework, but short of z-index, I was coming up empty on something to cover for this final post of my A-Z series – so let’s just call it poetic license. 

The “Geneva” Framework is just one part of this new claims-based identity platform, which also comprises “Geneva” Server and Windows CardSpace “Geneva”.  Its two primary goals are to simplify the single sign-on process within the enterprise and to enable existing systems to interoperate with new hosted services and service-oriented architecture implementations.

"Geneva" Overview

Let’s take a brief look at the three components, and then I’ll offer a number of links for further reference.

 

“Geneva” Server

“Geneva” Server is a Security Token Service (STS) that transforms claims into tokens.  Clear as mud? let’s take a step back to get a handle on what that means.

A claim is simply a piece of information about a specific identity (often a user), and it’s the assembly of one or more of these claims into a (security) token that drives the authentication model.  A claim could be someone’s name, their age, a password, or really any other piece of information that’s relevant to identifying valid users to a given application or computing environment.  Tokens are interpreted by the application to which the user is requesting access, and it’s up to that application (the “relying party” in the picture above) to decide whether it trusts the provider of the token and the claims it is making.

While the specific claims aren’t all that important, knowing those claims are accurate and valid is paramount, so the token (which contains the claims) is digitally signed by a trusted party to verify its source and guard against tampering.  The issuer of a signed token is referred to as a Security Token Service (STS), and the organization hosting the STS (the “trusted party”) is termed the identity provider – they are the ones attesting that the claims in the tokens they issue are valid.  In the Windows Live ID service, for instance, Microsoft is acting as the identity provider.

“Geneva” Server, which is the next generation of Microsoft’s Active Directory Federation Services (ADFS), implements just such a Security Token Service.   An organization could then choose “Geneva” Server to implement their own claims-based identity provider, built on Active Directory Domain Services, and act as an identity provider to applications within their enterprise or elsewhere.

 

Windows CardSpace “Geneva”

Cardspace “Geneva” is the user-focused component of the “Geneva” platform.  In the simplest of scenarios, a user would have one identity, managed by an STS like “Geneva” Server, and she’d request the STS to provide the claims associated with her identity to authenticate herself to every application. 

That’s a rather myopic view though.  Just as we have multiple identification facets in our physical lives (social security numbers, driver’s licenses, passports, etc.) that we use in different contexts, so do we have multiple digital identities that we use for different applications and services (think for instance of all of the various passwords you’re keeping track of now!)  My Facebook account, for instance, doesn’t need to know what my Blue Cross number is, but my on-line pharmacy probably does.

This is where the concept of an identity selector comes in.  When a user requests the services of an application, that application will forward a list of its needs – in terms of token types, claim requirements, and the identity providers it trusts.  The identity selector, here “Geneva” Cardspace, can review the requirements and present the user a list of the available identities (presented as Information Cards) that meet the authentication needs of the application.  The information card contains the information needed to locate the appropriate STS and obtain the requisite token; the card itself doesn’t store any claims or token information.

“Geneva” Cardspace also handles the user interface, so that there is a consistent experience across Windows applications relying on identity selection.  The interface presented to the user looks something like the following, with a link (“What information will be sent”) to provide more information on exactly what specific pieces of information (claims) will be sent to the relying party application.

 

CardSpace User Interface

 

“Geneva” Framework (previously “Zermatt”)

The “Geneva” Framework (my “Z” for this post) provides the programmatic interface for .NET developers to validate and consume claims in client applications (the “relying party” in the figure presented at the beginning of this post).  The .NET Framework 3.0 does include a few classes in System.IdentityModel.dll to do claims-based programming; however, the developer story wasn’t too compelling in terms of openness and ease of use.

At the forefront of the new framework (incorporated into Microsoft.IdentityModel.dll), is the Claim class, which contains several properties:

ClaimType

Typically a URI that indicates what the claim is.  This URI could be anything that your application and issuer can agree on.  In CardSpace, a user’s first name would have a ClaimType of http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname 

Value and ValueType

Value indicates the value of the claim, such as “Jim” for a first name.  This is always a string value.  The ValueType property indicates the underlying XML schema type used to serialize and deserialize the value. There’s a number of predefined schema type strings you can use in the static class ClaimValueTypes; for instance, a date value would be serialized per http://www.w3.org/2001/XMLSchema#date

Properties

An IDictionary<string, string> of optional metadata related to the claim.

Issuer and OriginalIssuer

A name for the issuer (STS) of the claim.  In a federated scenario, where two or more issuers are involved, the Issuer is the last in the chain, and OriginalIssuer is the first in the chain.

Subject

An object implementing IClaimsIdentity that provides additional information about the user’s identity.  IClaimsIdentity extends the existing IIdentity interface, which provides only the name of the user.  IClaimsIdentity exposes a ClaimsCollection as well as a way to specify which claim corresponds to the user’s name and roles.

The “Geneva” Framework also exposes a IClaimsPrincipal interface, with a default implementation in ClaimsPrincipalIClaimsPrincipal extends the existing IPrincipal class to include a collection of IClaimsIdentities.  In most, cases there will be one item in this collection, but it does make it possible to support scenarios where the relying application requests multiple tokens from different issuers.

The addition of the new interfaces and enhancement of the Claims class does add a little more complexity, but given the extension of existing interfaces, you can access the claims information using the same constructs you’ve used before to get the user information, by accessing the Thread.CurrentPrincipal or User.Identity in an ASP.NET page.  For example,

IClaimsIdentity claimsIdentity = 
Thread.CurrentPrincipal.Identity as IClaimsIdentity;

The Microsoft Code Name "Geneva" Framework Whitepaper for Developers is a great source of information for using these classes within your applications. In fact, it’s interesting (and not too surprising) to note that “Geneva” Server actually uses the “Geneva” Framework itself to implement its own security token service.  If you’re concerned over all the infrastructure required to try all this out, you’ll be happy to know that if you download the Beta 2 release, there’s an option to get a pre-configured VPC to facilitate your evaluation.

One final point to note is that while these three components provide a solid infrastructure for claims-based identity, they do not form an all-or-nothing proposition.  “Geneva” is Microsoft’s implementation of standards-based technologies like WS-Trust and SAML 2.0.  You don’t have to use “Geneva” server to use CardSpace; in fact, you might not even need an identity selector for simple scenarios.  Likewise, applications built using the “Geneva” Framework can process claims served from sources other than “Geneva” Server.

 

References

MSDN Security Developer Center

“Geneva” Claims Based Access Platform (whitepapers)

“Geneva” Framework Whitepaper for Developers

“Geneva” Beta 2 Download

Channel 9: The Id Element

“Geneva” Team Blog

Talk about timing! We just finished up our Summer 2009 Northeast Roadshow Series yesterday at Farmington (thanks for all that came out!), and today there’s a huge announcement relevant to my Windows 7 presentation.  Pricing for the retail Windows 7 versions was just announced, and here’s the estimated figures for the US:

 

Edition Upgrade Price Full Price
Home Premium $119.99 $199.99
Professional $199.99 $299.99
Ultimate $219.99 $319.99

 

But as folks say, "why pay full price" (even when it’s 10% or more lower than Vista)?  Here’s two upgrade programs you’ll definitely want to consider:

Windows 7 Upgrade Option Program

Buy a PC (tomorrow, June 26th, through January 31st 2010) with Vista Home Premium, Business or Ultimate, from a participating OEM or retailer, and receive a free (or low cost) upgrade to the corresponding version of Windows 7.  Get the details here.

Pre-order program

Beginning tomorrow, June 26th, US and Canada residents can pre-order Windows 7 Home Premium (for $49.99) or Professional (for $99.99) – more than a 50% discount.  Visit select on-line partners like Best Buy and Amazon, or the Microsoft Store – tomorrow through July 11th.

Also, check out the Windows Team Blog posting for similar offers in other markets.

Windows 7 Release Candidate Well, it’s really August 1st, but if you’re still running Windows 7 Beta, you’ll see your machine spontaneously rebooting every two hours beginning next week.   While that may be a great way to rest your eyes and carpals, you’ll probably find it a bit less intrusive to install the Release Candidate.

But do so quickly, the Release Candidate is available for download only until August 15th.  Of course, if you’ve been to (or are coming to) our Northeast Roadshow, you’re all set since we’ve been handing out DVDs of Windows 7 RC there!

My colleagues in the New York City metro region, Peter Laudati and Dmitry Lyalin, have launched a new podcast called The Connected Show, focused on the latest Microsoft developer technologies with a eye (or is that ear?) on interoperability with non-Microsoft technologies.

The latest episode features Ted Neward talking with Peter about Project Stonehenge and Java/.NET interoperability.  Dmitry and Peter also talk with Andrew Brust on the discontinuation of the Oracle ADO.NET provider, and Peter shares his thoughts on JavaOne.

Check it out, and be sure to let them know how you like the podcast.

y

Before we talk specifically about the yield keyword, let’s review a few constructs you probably use everyday, namely collection classes like lists and arrays.  We’re quite used to traversing these simply with a foreach loop, and what enables us to do so is that these types implement the System.Collections.IEnumerable interface.

IEnumerable is a rather simple interface that requires implementing a single method, GetEnumerator, which returns an object that implements another interface, IEnumerator. IEnumerator, in turn, encapsulates two methods and a property:

MoveNext called when advancing to the next element in the sequence or collection; it returns false when at the end of the collection.
Current obtains the System.Object at the current spot in the collection
Reset reinitializes the list or sequence

 
Now, let’s say we wanted to build a implementation of a class that returns the first n triangular numbers.  One way to do so would be the following:

   1: public class TriNumbers : IEnumerable  
   2:      {   
   3:          private Int32[] _nums;  
   4:          public TriNumbers(Int32 n)   
   5:          {
   6:              if (n <= 0) 
   7:                  throw new System.ArgumentOutOfRangeException();
   8:  
   9:              _nums = new Int32[n];   
  10:              _nums[0] = 1;   
  11:              for (Int32 i = 1; i < n; i++)  
  12:                  _nums[i] = _nums[i-1] + i + 1;  
  13:          } 
  14:         
  15:          public IEnumerator GetEnumerator() 
  16:          {  
  17:              return _nums.GetEnumerator();  
  18:          }  
  19:      }

and then iterate over it with code like this. 

class Program
{
static void Main(string[] args)
{
TriNumbers triNums = new TriNumbers(10);
foreach (var val in triNums)
Console.WriteLine(val);
Console.ReadLine();
}
}

In the implementation of TriNumbers, I sort of ‘cheated’ (line 17) by just deferring the iteration work to the iterator I get for free from the underlying integer array, which is really what’s storing my sequence. 

Not that cheating is wrong in this case, but it does require my array to be populated from the get-go, in the constructor.  Initializing an array of ten elements is no big deal, but what if the argument to the constructor were much larger? and there are many instances of this class in memory?  and what if I only end up iterating over the first couple of values?  Clearly I’m using up memory here that isn’t really needed, and I’m taking time to calculate each element of the sequence, when I’m not really even sure I need them all.  If the calculation was CPU-intensive or required a series of database or services calls to build the list, there could be a lot of wasted cycles dedicated to populating data that might never be requested in the application.

To make the class a little less wasteful up front, I can implement the IEnumerator interface explicitly – something like this:

public class TriNumbers : IEnumerable
{
private TriNumbersEnumerator _enum;
public TriNumbers(Int32 n)
{
_enum = new TriNumbersEnumerator(n);
}

public IEnumerator GetEnumerator()
{
return _enum;
}

private class TriNumbersEnumerator : IEnumerator
{
private Int32 _limit = 0;
private Int32 _index = 0;
private Int32 _value = 0;
public TriNumbersEnumerator(Int32 n)
{
if (n <= 0)
throw new System.ArgumentOutOfRangeException();
_limit = n;
}

public object Current
{
get
{
if (_index == 0)
throw new System.InvalidOperationException(
"Enumeration has not started. Call MoveNext.");
else
return _value;
}
}

public bool MoveNext()
{
_index++;
if (_index <= _limit)
{
_value += _index;
return true;
}
else
{
Reset();
return false;
}
}

public void Reset()
{
_index = 0;
_value = 0;
}
}
}

That works, but there’s an easier way!  Enter yield – not much longer than the original implementation, and certainly less wasteful of space and CPU cycles should the entire sequence not be enumerated.

   1: public class TriNumbers : IEnumerable
   2: {
   3:     private Int32 _limit = 0;
   4:     public TriNumbers(Int32 n)
   5:     {
   6:         if (n <= 0)
   7:             throw new System.ArgumentOutOfRangeException();
   8:         _limit = n;
   9:     }
  10:  
  11:     public IEnumerator GetEnumerator()
  12:     {
  13:         Int32 val = 0;
  14:         for (Int32 i = 0; i < _limit; i++)
  15:         {
  16:             val += i + 1;
  17:             yield return val;
  18:         }
  19:     }
 
The magic is on line 17, with the yield return statement.  Each time this statement is reached, the current value of val is returned as the value of the IEnumerator reference (namely, what the foreach in the calling program will see).  The current location and state of the GetEnumerator method is stored, and so the next time the iterator is called, we’ll get the next value from the for loop in line 14.

While reading IL is not something I do often, it’s interesting to look at what gets generated when using the yield return construct.  Using IL DASM, you can see a newobj call to a class called d__0, essentially indicating that there is a new class being constructed under the covers. IL DASM output

IL DASM shows that that class is implementing the IEnumerator interface (the Current property and the Reset and MoveNext methods), so essentially the yield return provides some "syntactic sugar" (more or less) for code similar to the TriNumbersEnumerator class that I wrote above. 

IL DASM


The real story is a tad deeper than that though, and Wes Dyer does a great job of looking more closely at the generated class in his blog post.  And if you’d like to do all this in Visual Basic, which unfortunately doesn’t have iterators (yet?), take a look at Matthew Doig’s blog.

Looking to dive into coding with Windows 7?  The RC Training Kit is a great resource for developers, with hands-on labs and presentation materials covering many of the Windows 7 features, including:

  • Windows 7 Taskbar
  • Libraries and Federated Search
  • Multi-touch
  • Sensor and Location APIs
  • Ribbon
  • Background Services and Tasks
  • Instrumentation and Performance

There’s also the Windows® API Code Pack for Microsoft® .NET Framework, currently at version 0.90, which provides a namespace of managed code wrappers for many, but not yet all, of the COM and C++ interfaces exposed by the new Windows 7 features.

You’ll note that the examples in the RC Training Kit use a different set of managed code wrappers from what’s in the API Code Pack.  There’s an on-going plan to port most of the older Vista Bridge code (in the training kit) to the API Code Pack, but until the Code Pack reaches its version 1.0 RTM milestone, you may end up dealing with one (or both) managed code layers depending on what features of Windows 7 you’re working with and what code samples you’re consulting.

X

XML Literal(s) is a feature that Visual Basic developers can call their own! The XML Literal syntax facilitates creating XML documents and elements that support the vast majority of the XML 1.0 specification. Complementing the literals are axis properties that aid in navigating and accessing XML elements and attributes.

Let’s start with an example.  Below is an XML literal representing a menu; as you can see it’s assigned directly to a variable – here relying on implicit typing.

Dim xml = <menu>
<course name="appetizer">
<dish>Shrimp Cocktail</dish>
<dish>Escargot</dish>
</course>
<course name="main">
<dish>Filet Mignon</dish>
<dish>Garlic Potatoes</dish>
<dish>Broccoli</dish>
</course>
<course name="dessert">
<dish>Chocolate Cheesecake</dish>
</course>
</menu>

So what is the type of the xml variable?  It’s a System.Xml.Linq.XElement, which certainly brings to the forefront the synergy with LINQ (Language Integrated Query).  Underneath the covers, the Visual Basic compiler calls the relevant constructors (XElement, XAttribute, etc.) to create the LINQ to XML object.

Note though that xml in the following snippet is an XDocument, because of the inclusion of the xml version tag.

Dim xml = <?xml version="1.0"?>
<menu>
<course name="appetizer">
<dish>Shrimp Cocktail</dish>
<dish>Escargot</dish>
</course>
</menu>


LINQ within XML Literals

You can include LINQ expressions with your literals as well, using an embedded expression syntax reminiscent of ASP!  The following code yields the same XML as the first example, but populates it using LINQ to Objects from a hard-coded list; of course, that data could just as easily come from a database or some other external source.

Dim menu As List(Of Dish) = New List(Of Dish)()

menu.Add(New Dish("appetizer", "Shrimp Cocktail"))
menu.Add(New Dish("appetizer", "Escargot"))
menu.Add(New Dish("main", "Filet Mignon"))
menu.Add(New Dish("main", "Garlic Potatoes"))
menu.Add(New Dish("main", "Broccoli"))
menu.Add(New Dish("dessert", "Chocolate Cheesecake"))

Dim xml = <?xml version="1.0"?>
<menu>
<course name="appetizer">
<%= From m In menu _
Where m.Course = "appetizer" _
Select <dish><%= m.Food %></dish> _
%>
</course>
<course name="main">
<%= From m In menu _
Where m.Course = "main" _
Select <dish><%= m.Food %></dish> _
%>
</course>
<course name="dessert">
<%= From m In menu _
Where m.Course = "dessert" _
Select <dish><%= m.Food %></dish> _
%>
</course>
</menu>

XML literals can also be traversed via LINQ to XML.  For instance, the following snippet will result in a list of each of the dishes, without any XML markup.

Dim dishList = From d In xml.Descendants("dish") _
Select d.Value
For Each item In dishList
Console.WriteLine(item)
Next

 

Axis Properties

Axis properties are a series of syntax elements that enable you to access elements and attributes within the XML.  There are three different properties that we’ll take a look at next.

Child axis

The child axis property returns all child elements of the source element.  The property is denoted by placing angle brackets around the child elements desired.  For example, this code

Dim childAxis = xml.<menu>.<course>
For Each item In childAxis
Console.WriteLine(item)
Next

results in

<course name="appetizer">
  <dish>Shrimp Cocktail</dish>
  <dish>Escargot</dish>
</course>
<course name="main">
  <dish>Filet Mignon</dish>
  <dish>Garlic Potatoes</dish>
  <dish>Broccoli</dish>
</course>
<course name="dessert">
  <dish>Chocolate Cheesecake</dish>
</course>

Descendant axis

The descendant axis property returns all elements under the source element, regardless of how deeply they are nested.  This property is denoted by placing angle brackets around the element desired and separating it from the source element with three periods.  For example, the following code produces the same XML as shown for the child axis example above.

Dim childAxis = xml...<course>
For Each item In childAxis
Console.WriteLine(item)
Next

You can use indexing as well, for instance, to get only the last course, dessert, via the following line

Dim course3 = xml...<course>(2)

Attribute axis

The attribute axis returns the specified attribute (denoted by the @ symbol) of the element on which it operates.  For instance, the following code will print out the three courses: appetizer, main, and dessert.

Dim childAxis = xml...<course>
For Each item In childAxis
Console.WriteLine(item.@name)
Next

 

For more information on using XML Literals with Visual Basic, check out the MSDN topic, XML in Visual Basic.  And for those of you using C#, there’s a neat little add-in that will paste the code necessary to create the XML document that you have stored on the clipboard.  Here’s how:

  1. Extract the LinqSamples\PasteXmlAsLinq directory from
    C:\Program Files\Microsoft Visual Studio 9.0\Samples\1033\CSharpSamples.zip
  2. Build the PasteXmlAsLinq solution
  3. Copy the PasteXmlAsLinq.dll and PasteXmlAsLinq.Addin files into the Addins folder.  This folder is located at My Documents/Visual Studio/Addins, but you may need to create it explicitly.
  4. Restart Visual Studio.
  5. If you have XML in the clipboard (perhaps copied from some other source), you’ll see a Paste XML as XElement menu option in the Edit menu:

Paste XML as XElement

Copying the XML literal syntax from the first Visual Basic example in this post into the clipboard and then pasting into a C# script using the menu item above yields the following:

XElement xml = new XElement("menu",
new XElement("course",
new XAttribute("name", "appetizer"),
new XElement("dish", "Shrimp Cocktail"),
new XElement("dish", "Escargot")
),
new XElement("course",
new XAttribute("name", "main"),
new XElement("dish", "Filet Mignon"),
new XElement("dish", "Garlic Potatoes"),
new XElement("dish", "Broccoli")
),
new XElement("course",
new XAttribute("name", "dessert"),
new XElement("dish", "Chocolate Cheesecake")
)
);

It’s not exactly an XML literal, but certainly saves a lot of typing!

I don’t casually throw around words like ‘awesome', but this is indeed it.  If you’re building applications for WPF and/or Silverlight, you’ve certainly become XAMLized, since XAML (eXtensible Markup Language) is the “common” user-interface description markup.   Well, more-or-less “common”.  Both technologies use XAML, but the level of support has subtle nuances, and things that are supported on WPF may not be supported on Silverlight (though 99% of Silverlight is supported by WPF).  It’s not always obvious what the nuances are, and like me, you may have discovered them empirically – and uttered a few words of frustration not printable here.

The good news is that WPF and Silverlight are aligning, so “we feel your pain,” and the development process and support will continue to converge.  Until we reach that nirvana though, the WPF-Silverlight Comparison Whitepaper is a MUST read for anyone embarking on (or currently developing) WPF and Silverlight applications.  It’s a comprehensive 69-page document on just those nuances between the technologies that we prefer to know about before designing and coding – versus after!

The document was produced by our friends at Wintellect at the behest of Microsoft, and is, well, an awesome reference for developers in this space.

W

With most of the major development technologies from Microsoft beginning with a “W” – Windows Presentation Foundation, Windows Communication Foundation, Windows Workflow Foundation – there was no shortage of choices for today’s post.  But somehow those just seemed too easy, and well, a bit large to tackle in one post.

So, from the depths of the .NET Framework, I settled on a class called WeakReference.  A weak reference is essentially a reference to an object that you can access but that is also a candidate for garbage collection.  It’s essentially a shade of grey between the absolutes of having an object in memory that has a strong reference (e.g., you’ve got a variable referencing the object) and an object that’s been marked for garbage collection but hasn’t yet been reclaimed.

By creating a weak reference to an object, you’re saying that you’d like to still be able to use it in the future, but it’s not *that* important that it can’t be reclaimed by the garbage collector – in which case, you’ll take the hit for recreating it.

Let’s take a look an example here. It’s a simplified version of the sample from the MSDN reference page, and dare I say, a bit more interesting, since it forces a garbage collection so that you can actually see the weak reference scenario kick in.

The context here is a cache, specifically a wrapper for a Dictionary<int, String>; of course, the String could be any object here.  The strings are initialized to just the word “Item” followed by their index in the dictionary.  The indexer (line 15) casts the weak reference to a strong one in line 19 to determine whether the object is still available.  If the null test (line 22) fails, we know the object has been garbage collected, and we have to create a new one (line 24).

Note, there is an IsAlive property on WeakReference which will return true if the object reference has not yet been garbage collected; however, be aware that in the instant between the positive test for IsAlive and the actual reference of the object, that object could be garbage-collected.  It’s recommended that you do the cast and then check for null as I did below.

   1: public class Cache
   2: {
   3:     Dictionary<int, WeakReference> Items;
   4:     public int Count { get { return Items.Count;} }
   5:  
   6:     String ItemFactory(int i) { return "Item " + i.ToString(); }
   7:  
   8:     public Cache(int count)
   9:     {
  10:         Items = new Dictionary<int, WeakReference>(count);
  11:         for (int i = 0; i < count; i++)
  12:             Items[i] = new WeakReference(ItemFactory(i), false);
  13:     }
  14:  
  15:     public String this[int index]
  16:     {
  17:         get
  18:         {
  19:             String d = (String) Items[index].Target;
  20:             Console.WriteLine("Object at {0,2}: {1}", index.ToString(), 
  21:                 d == null ? "Regenerated" : "Original");
  22:             if (d == null)
  23:             {
  24:                 d = ItemFactory(index);
  25:                 Items[index] = new WeakReference(d, false);
  26:             }
  27:             return d;
  28:         }
  29:     }
  30: }

Now, let’s take a look at a simple console application to show the weak references in action.  I create a Cache object with ten items, and then launch a loop that references items in the cache ‘randomly’ (using the random number generator reference created in line 3).  The twist here is that on line 13, a garbage collection is forced on every fifth reference to the cache.  That will result in all of the current weak references in the cache being garbage collected.

   1: public static void Main()
   2: {
   3:     Random r = new Random();
   4:     Cache c = new Cache(10);
   5:  
   6:     // Randomly access objects in the cache.
   7:     for (int i = 0; i < 100; i++)
   8:     {
   9:         Console.Write("{0,2}: ", i);
  10:  
  11:         String s = c[r.Next(c.Count)];
  12:  
  13:         if ((i + 1) % 5 == 0) GC.Collect();
  14:     }
  15:     Console.ReadLine();
  16: }

So, lets take a look at the output, below.  The first five lines demonstrate accessing the weak references set up in the cache constructor; no garbage collection has yet occurred.  The console outputgarbage collection occurs at the end of the fifth iteration, and all the weak references in the cache are reclaimed.  As a result, when the 3rd item is referenced at iteration 5, the strongly cast reference to

Items[index].Target

(line 19 in the first code snippet above) results in null, and a new weak reference is subsequently re-created.  On iteration 8, we get lucky, because the object at index 0 was just recreated in iteration 6.

One thing this example drives home is that the effectiveness of this approach in terms of implementing a cache pattern depends on the frequency of the garbage collection.  Here, I forced garbage collection for the sake of example, but it could well be the case that the frequency and cost of re-creating objects outweigh the benefits that the cache was to provide.  Jeffrey Richter in CLR via C# cautions:

The problem with this technique is the following: Garbage collections do not occur when memory is full or close to full.  Instead, garbage collections occur whenever generation 0 is full, which occurs approximately after every 256 KB of memory is allocated.  So objects are being tossed out of memory much more frequently than desired, and your application’s performance suffers greatly.

… Basically you want your cache to keep strong references to all of your objects and then, when you see that memory is getting tight, you start turning strong references into weak references…some people have had much success by periodically calling the Win32 GlobalMemoryStatusEx function and checking the returned MEMORYSTATUSEX structure’s dwMemoryLoad member.  If this member reports a value above 80, memory is getting tight, and you can start converting strong references to weak references based on whether you want a least-recently used algorithm, a most-frequently used algorithm, a time-base algorithm, or whatever.

If you look closely at the documentation for WeakReference, you’ll note there is a property called TrackResurrection as well as an overloaded constructor to set this value when creating the weak reference.  The default value, which I relied on above, is false, indicating a short weak reference.  The alternative, a long weak reference, allows the object to be reclaimed even after finalization

If your object has a finalizer (or destructor), the garbage collection process actually has to ‘resurrect’ the object to allow the finalizer to run.  If you have a long weak reference, you’re allowed to reclaim the object at this stage; with a short weak reference, you can’t.  Note that if you did reclaim a long reference at this point, the object has been finalized, so you may end up with an object that has an indeterminate state in terms of the unmanaged resources that the finalizer was cleaning up.  To cut to the chase, safe use of long weak references is not for the meek!

CT.NET Developers GroupThe tentative schedule of presentations for the 2nd CTDOTNET CodeCamp has been posted, and it looks like a full day with nearly three dozen sessions in tracks like WCF, Java and jQuery, Agile, Database, WPF and Silverlight, and more.

If you haven’t signed up to attend yet, here’s the link to do so.  The CodeCamp is being held at the New Horizons Computer Learning Center in Bloomfield, and runs from 9 a.m. to 5:30 p.m.

More Posts Next page »
 
Page view tracker