# Tuesday, September 8, 2009

Feeds are part of what power Web 2.0. You can use RSS or ATOM to syndicate your content and allow readers to subscribe to your content. It is what powers Twitter, Facebook, as well as CNN and the New York Times. ATOM is a popular alternative to RSS. Just about every blog and “RSS” reader will support ATOM. When you are talking about ATOM, you actually are talking about two things: Atom Syndication Format, an XML language for feed definitions, and the Atom Publishing Protocol, a very simple HTTP protocol for creating and updating feed based content.

The WCF REST Starter Kit allows you to create services that will produce an ATOM feed or expose your data as a service via the ATOM Publishing Protocol. The WCF REST Starter Kit gives you a Visual Studio template to get your started.

You can use Telerik OpenAccess as a data source for the collections of your ATOMPub service. To do so you have to wire up some code in your svc file back to your OpenAccess entities. This can be accomplished by using the OpenAccess WCF Wizard.

Creating the ATOMPub Service and a Silverlight Front End

To get started, let’s create four projects in Visual Studio, one data access layer using OpenAccess, one AtomPub service project using the new WCF REST Toolkit Visual Studio template, and a Silverlight Web/Client.

image

Then you can use the Telerik OpenAccess WCF Wizard to automatically create the SVC and CS files for an ATOMPub service (the arrow above.) Once you have that and allow the project to talk to the DAL with a reference to that project, view the service in the browser for a reality check. You can do the format, similar to REST of: http:// server name / service name / resource name, for example:

http://localhost:54669/NorthwindAtomPubService.svc/Customers will return a list of all the customers and if you add an /CustomerID to the end of the URL like this: http://localhost:54669/NorthwindAtomPubService.svc/Customers/ALFKI then you will bring up one individual customer.

image

Now let’s consume this from a Silverlight application. Pretty easy stuff, but first we have to create a XAML grid:

<data:DataGrid x:Name="dataGridCustomers" 
               AutoGenerateColumns="False" ItemsSource="{Binding}">
    <data:DataGrid.Columns>
        <data:DataGridTextColumn 
            Binding="{Binding Path=CompanyName}" Header="Company Name">
        </data:DataGridTextColumn>
        <data:DataGridTextColumn 
            Binding="{Binding Path=ContactName}" Header="Contact Name">
       </data:DataGridTextColumn>
    </data:DataGrid.Columns>
</data:DataGrid>

After you build your XAML grid, let’s set a service reference to the AtomPub service and then start to write some code. Just like before, I will have a LoadData() method to fill the grid. Of course being Silverlight it has to be asynchronous. Here is the code.

   1:  private void LoadData()
   2:  {
   3:  //the URL of our ATOM Pub service
   4:  string uri = "http://localhost:54669/NorthwindAtomPubService.svc/Customers";
   5:  //set up the web request
   6:  HttpWebRequest request = HttpWebRequest.Create(
   7:      new Uri(uri)) as HttpWebRequest;
   8:  //HTTP GET (REST uses the standard HTTP requests)
   9:  request.Method = "GET";
  10:  //begin the async call in a code block
  11:  request.BeginGetResponse(ar =>
  12:      {
  13:          Dispatcher.BeginInvoke(() =>
  14:              {
  15:                  //catch the HTTPWebRequest
  16:                  HttpWebResponse response = 
  17:                      request.EndGetResponse(ar) as HttpWebResponse;
  18:                  //get the customers back
  19:                  var result = 
  20:                      Customer.GetCustomersFromAtom20Stream(response.GetResponseStream());
  21:   
  22:                  //stuff the customers into a LIST
  23:                  List<Customer> customers = new List<Customer>();
  24:   
  25:                  foreach (var customer in result)
  26:                  {
  27:                      customers.Add(customer);
  28:                  }
  29:                  //bind the LIST to the Silverlight DataGrid
  30:                  dataGridCustomers.DataContext = customers;
  31:              });
  32:      }, null);
  33:  }

 

This code is easier than it looks. We start off with a HTTPWebRequest for the service (line 6-7) using an HTTP GET (line 9) and a code block to handle the async call (starting on line 11) . This code block works similar to a callback method. Inside the code block we catch the HTTPWebRequest asynchronously and get the results into the implicitly typed local variable var on line 19. After that is just basic LIST stuff, filling the list and adding it as the source of the dataGrid.

image

Pretty easy. Since that was so easy, let’s shake it up a little bit. How about we make the backend database be SQL Azure instead of SQL Server 2008.

Connecting Telerik OpenAccess to SQL Azure

Turns out that using SQL Azure and Telerik OpenAccess is pretty easy. If you have an Azure schema that is the same as your SQL Server 2008 schema, all you have to do it change the connection string in the OpenAccess DAL project’s app.config.  Let’s change our connection string in the DAL project to use SQL Azure like this:

   1:  <connection id="Connection.Azure">
   2:    <databasename>Northwind_Lite</databasename>
   3:    <servername>tcp:tpzlfbclx123.ctp.database.windows.net</servername>
   4:    <integratedSecurity>False</integratedSecurity>
   5:    <backendconfigurationname>mssqlConfiguration</backendconfigurationname>
   6:    <user>Stevef</user>
   7:    <password>gomets</password>
   8:  </connection>

 

Line 3 is the server you get from the SQL Azure CTP program and the user name and password is what you set up when you got the CTP. (Don’t have a SQL Azure CTP? Sign up here!)

That is it. When I run it I get the same results, except that the data is coming from SQL Azure and not from SQL Server 2008. I went into my Northwind_Lite database in SQL Azure and edited the first Customer row so I always know the data is coming from SQL Azure:

Update Customers 
Set CompanyName= 'Alfreds Futterkiste SQL Azure'
Where CustomerID='ALFKI'

Now when I run the project, I see the data from SQL Azure:

image

That is all there is too it!

You can get the WCF REST Starter Kit here, the Telerik OpenAccess WCF Wizard here, and the code from this blog post here.

Enjoy!