Posts tagged OData

Presenting on OData and WCF 4 at St Louis Day of .NET

If you haven’t registered, you should join the 600+ people who have registered for the 2010 St Louis Day of .NET, making this our biggest year yet!

I will have two presentations that are scheduled for both days of the conference.  Kicking off the morning with the JumpStart series, I will be delivering the WCF 4 JumpStart.  In this session we’ll cover the basics of WCF 4: What’s new/changed from 3.5, REST Support, the Routing Service and Deployment to IIS on multiple bindings (BasicHttp, Rest, and Net.Tcp).

The OData talk will be the same one I have delivered at Iowa Code Camp and the St Louis .NET Users Group.  If you haven’t seen the session covers the basics of OData, how to query OData sources, and finally we’ll build an OData service following the Hanselman Stackoverflow Challenge.

Beyond my small contributions there are over a 100 great sessions to look for.  Check out the agenda, our speakers, and try out the schedule builder to build a personalized schedule!

Speaking at the June STL .NET User Group

This month I will be presenting "OData – Make a Feed for That" at the St. Louis .NET User Group.

From the website:

You might have heard of ADO.NET Data Services and are wondering why it’s now WCF Data Services (Or maybe you haven’t heard of either which is another reason to attend this month’s user group!). In this session we’ll explore the OData protocol that drives WCF Data Services and look at example OData services and how you can consume them easily in .NET and Silverlight. Join us and learn how OData can make the web your data source with this new standard.

As always, there is *free food* so you can come chew on that and tune out the speaker if that’s how you roll!

Chris is a human. From planet earth. He likes to watch his kids grow and his disposable income shrink. For 40 hours a week he is a consultant with Daugherty Business Solutions in the Custom Line of Service working on .NET projects. The rest of the time Chris is chasing his kids, spending time with his wife or playing Xbox (or all of the above at the same time). He also likes to speak at local user group meetings and conferences about Microsoft technologies. He is a 2009 Microsoft MVP and does not look at all like a clown.

Monday, June 28, 2010
5:30 – 6:00 pm Food and social
6:00 – 7:30 pm Program

Location:
Three City Place Drive
Suite 1100
Creve Coeur, MO 63141

Iowa Code Camp – Slides & Links

Thanks to all who attended the OData session at Iowa Code Camp!  Here are the slides & links to more info about OData.

Links:

Pretty URIs in WCF Data Services – Lose the .svc File

I’m still testing this, but, it looks like in .NET 4 you will be able to use the new URL Routing to lose the .svc file in your WCF Data Services.

Step-by-step

Follow the standard steps for creating a WCF Data Service.

  • Create a new, “Empty ASP.NET Web Application”
  • Add an Entity Data Model

Once you have your entity data model, add a new class, derive it from DataService<T> where T is the entity set you created in the previous steps:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Services;
using System.Data.Services.Common;

namespace ODataSample
{
    public class ProductService : DataService<AdventureWorksLT2008Entities>
    {
        public static void InitializeService(DataServiceConfiguration                                                  config)
        {
            config.DataServiceBehavior.MaxProtocolVersion =                  DataServiceProtocolVersion.V2;
            config.SetEntitySetAccessRule("Products",                 EntitySetRights.AllRead);
            config.SetEntitySetPageSize("Products", 20);
        }
    }
}

Add a Global Application Class (global.asax).  In the Application_Start method add the following snippet:

RouteTable.Routes.Add(new ServiceRoute("ProductCatalog", new WebServiceHostFactory(), typeof(ProductService)));

If all is well you should be able to hit F5 to debug.  Since there is not a default page you’ll have to add the route name you specified onto the end of the URI.  If all goes well you should see:

image

In my testing thus far it appears everything works as you’d expect in terms of appending on query parameters, etc. 

image

image

Using Silverlight 4 to Browse NetFlix’s OData Catalog

One of the announcements at Mix 10 was the preview of NetFlix’s OData catalog API.  The Astoria team has been hard at work getting the new release of what is now called WCF Data Services (Formerly ADO.NET Data Services).  In Silverlight 4 the WCF Data Services Client is included as part of the Silverlight runtime which makes it very easy to consume OData Services.

OData?

OData is the Open Data Protocol which Microsoft created originally for ADO.NET Data Services.  The protocol was adopted by enough people that Microsoft focused more effort on it.  OData defines the structure & types for publishing data using HTTP and an Xml Format based on Atom (AtomPub) or JSON.  OData has roots in REST and focuses on using the HTTP protocol to enable easy data access.

OData defines methods to enable querying data sources using structured URIs.

WCF Data Services & Silverlight 4

With Silverlight 4 you can directly add service references to WCF Data Services and the models for the catalog are generated automatically.  The results of the add service reference yields this:

image

image

Once you have the service reference you can work with the data using the NetflixCatalog class which is a DataServiceContext class.  The context generated classes includes DataServiceQuery<T> properties for the main resources in the Netflix OData catalog.

 public partial class MainPage : UserControl
    {
        private DataServiceCollection<Genre> _genres;
        private Genre _selectedGenre;
        private NetflixCatalog _context;

        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            _context = new NetflixCatalog(new Uri               ("http://odata.netflix.com/Catalog/"));

            _genres = new DataServiceCollection<Genre>();
            _genres.LoadCompleted += Genres_Loaded;

            var query = (from g in _context.Genres orderby g.Name select g);
            _genres.LoadAsync(query);
        }

If you haven’t done a lot with events and delegates then jumping into Silverlight will be tricky at first.  To load all the genres, we use an event handler which will bind to ComboBox.  This also handles “Continuations”, which is the mechanism Data Services use to enable paging across large datasets.  With the following code we will continue loading genres until we have loaded all of them.

        private void Genres_Loaded(object sender, LoadCompletedEventArgs e)
        {
            if (_genres.Continuation != null)
            {
                _genres.LoadNextPartialSetAsync();
            }
            else
            {
                LoadingGenresLabel.Opacity = 0;
                GenreCombobox.ItemsSource = _genres;
                GenreCombobox.UpdateLayout();
            }
        }

Once all the genres are loaded in the ComboBox you can select one and it will load the titles for that genre.  To load the Titles for a Genre we can use the following code:

        private void GenreCombobox_SelectionChanged(object sender,         SelectionChangedEventArgs e)
        {
            _selectedGenre = GenreCombobox.SelectedItem as Genre;
            if (_selectedGenre != null)
            {
                if (_selectedGenre.CatalogTitles.Count == 0)
                {
                    CatalogTitleListBox.SelectedIndex = -1;
                    _selectedGenre.CatalogTitles.LoadCompleted +=                       GenreCatalogTitles_Loaded;
                    _selectedGenre.CatalogTitles.LoadAsync();
                    LoadingTextBlock.Opacity = 1;
                }
                else
                {
                    CatalogTitleListBox.ItemsSource =                             _selectedGenre.CatalogTitles;
                    CatalogTitleListBox.SelectedIndex = 0;
                }
            }
        }

As you can see, the code for working with a data service to retrieve data is pretty straight-forward.  All the code for this project can be found on CodePlex: Silverlight 4 Netflix.  If you have Silverlight 4 installed you can try Silverlight4Netflix out here.  Otherwise, enjoy the screenshots below!

image

image

image

image