WCF 3.5: Uri Templates

14. August 2007 13:04

This is the first post in a long series about WCF 3.5.  Originally, I had intended to start with a different topic.  However, I gave it some thought and decided it would be a good idea to begin by covering some material that will appear throughout several of the new features.  You can think of these first few posts as building blocks toward future posts. 

As you could probably guess from the title of the post, I am going to cover Uri Templates. 

Before you start pulling your hair out trying to find the UriTemplate class, it requires a reference to the new library System.ServiceModel.Web.  However, the UriTemplate class logically resides in the System namespace.  Just keep in mind the physical assembly is System.ServiceModel.Web.

So, what exactly is a Uri Template?  At the end of the day, a Uri Template is nothing more than a parameterized uri.  It is simply a mechanism that allows a uri to be defined with placeholders rather than literal values.  Let's take a look at a couple of examples based on the .NET Framework documentation from MSDN Online:

{culture}/library/*
{culture}/library/{typeInfo}

In case you didn't know, the MSDN site is structured so that you can enter the write the uri as the namespace, type, and method or property to immediately access the corresponding documentation.  If you pull out the values that could potentially change, this can be "parameterized" into an Uri Template.

Now, you may have noticed the template doesn't contain a scheme, such as http, or a hostname.  This type of information is abstracted from the template and will vary depending upon the base address that is applied to the template at runtime.

Now, let's take a look at a few code examples of putting uri templates into action.  Here is an example that uses a template to create a uri to access the online documentation for the String.IsInterned method.:

// Setup a template.
UriTemplate template = new UriTemplate("{culture}/library/{typeInformation}");

// Define the base address.
Uri baseAddress = new Uri("http://msdn2.microsoft.com/");

// Define the parameters by name.
NameValueCollection parameters = new NameValueCollection();
parameters.Add("culture", "en-US");
parameters.Add("typeInformation", "system.string.isinterned");

// Create the formatted uri.
Uri formattedUri = template.BindByName(baseAddress, parameters);

// Output: http://msdn2.microsoft.com/en-US/library/system.string.isinterned

This particular example relies on the matches based on the name of the parameters, but the same thing could be done by ordinal position as well.  Consider the following:

UriTemplate template = new UriTemplate("{culture}/library/{typeInformation}");

// Define the base address.
Uri baseAddress = new Uri("http://msdn2.microsoft.com/");

// Create the formatted uri.
Uri formattedUri = template.BindByPosition(
    baseAddress,
    "en-US",
    "system.string.isinterned");

// Output: http://msdn2.microsoft.com/en-US/library/system.string.isinterned

There is also the ability to perform matching between templates and an existing uri, but I will dive into the specifics of that in the next post.

While none of this is really groundbreaking in terms of functionality, there is one key thing to keep in mind.  The logic is now consolidated into the framework.  Otherwise, everyone would end up writing their own library of functions that essentially does the same thing, but the implementation would vary from system to system.

Although Uri Templates may not seem that interesting in isolation, they play a major role for enabling some of the new features that will be discussed in upcoming posts.

Comments

9/23/2007 9:15:08 PM #

http://

If it is possible and convenient to you, i think it is good that you could provide a simple working sample for every topic you share. Smile


Thanks

http:// |

Comments are closed

About Me

I'm a passionate software developer and advocate of the Microsoft .NET platform.  In my opinion, software development is a craft that necessitates a conscious effort to continually improve your skills rather than falling into the trap of complacency.  I was also awarded as a Microsoft MVP in Connected Systems in 2008, 2009, and 2010.


Can’t code withoutThe best C# & VB.NET refactoring plugin for Visual Studio
Follow jeff_barnes on Twitter

View Jeff Barnes's profile on LinkedIn

 

Shared Items

Disclaimer

Anything you read or see on this site is solely based on my own thoughts.  The material on this site does not necessarily reflect the views of my employer or anyone else.  In other words, I don't speak for anyone other than myself.  So, don't assume I am the official spokesperson for anyone.