Related Content

Creating Streamlined DataServiceContext Proxy Class to interact with your UGC OData Feed

Posted by Mike Iskiw on April 4, 2011
0 people like this tutorial

OData is a very rich open standard that aligns perfectly with how .NET produces LINQ Queries. You can generate a Proxy Class using visual studio to auto generate all strongly typed classes that need to be generated for your feed. You may wish to use a custom DataServiceContext to build your classes from scratch. This tutorial will explain the basics of getting this running.

In this Tutorial we will use the example where only one User Generated Content Definition is defined called “BlogEntries”.

Define Your Objects

Your objects must match the name and data type structure that was defined in UGC. In this example we will assume a BlogEntry contains a Title and a Body Field, both being Text.

Note: There are default properties that are added to each Type: ID, State, ModifiedOn and CreatedOn (see below)

 public class BlogEntries { public int ID { get; set; } public string State { get; set; } public DateTime ModifiedOn { get; set; } public DateTime CreatedOn { get; set; } public string Title { get; set; } public string Body { get; set; } } 

Define the Proxy Class

Now that we have our object defined for our blog we can define a proxy class that has a strongly typed DataServiceQuery property which uses the BlogEntries Object. This property will represent the Query used to query the specific “BlogEntries” Feed in the OData feed.

public class BlogProxy : DataServiceContext { private static string url = ""; public BlogProxy(string BlogRecordTypeName): base(new Uri(url)) { } public DataServiceQuery BlogEntries { get { return CreateQuery("BlogEntries"); } } } 

As you can see this Proxy is very simple and simply defines the path to the OData UGC API service file and creates a BlogEntries property that creates a query against the “BlogEntires” feed.


Authentication Headers

Since UGC uses a specialized authentication mechanism we need to pass the credentials required to authenticate to the feed when calls are made. This is a useful scenario for the DataServiceContext Event “SendingRequest”. In this event we are able to append any additional HTTP Headers before the request if sent to the server. This will allow us to authenticate against the service as required.

We will modify our constructor defined in the BlogProxy class to automatically register this event and implement the method so all required authentication properties are set.

public BlogProxy(string BlogRecordTypeName): base(new Uri(url)) { this.SendingRequest += new EventHandler(BlogProxy_SendingRequest); } void BlogProxy_SendingRequest(object sender, SendingRequestEventArgs e) { //authenticate int profileRecordID = -1; string accessKey = ConfigurationManager.AppSettings["Agility_API_Key"]; string password = ConfigurationManager.AppSettings["Agility_API_Password"]; //get the number of seconds since 1,1,2001 TimeSpan elapsedSpan = DateTime.Now - new DateTime(2001, 1, 1); int seconds = Convert.ToInt32(Math.Floor(elapsedSpan.TotalSeconds)); Random _randomGenerator = new Random(); int randomNumber = randomNumber = _randomGenerator.Next(1, 1000); //[seconds].[recordID].[password].[accessKey].[random] string accessHash = BuildHash(accessKey, password, profileRecordID, seconds, randomNumber); //Add the HTTP Header values to the request e.RequestHeaders.Add("accessKey", accessKey); e.RequestHeaders.Add("hash", accessHash); e.RequestHeaders.Add("sec", seconds.ToString()); e.RequestHeaders.Add("randNum", randomNumber.ToString()); e.RequestHeaders.Add("profileID", profileRecordID.ToString()); } 

Using the Proxy Class to Interact with the OData Feed

Now that we have a proxy class that property authenticates against the OData Service Feed for the BlogEntries feed we are able to use the proxy and perform standard LINQ Queries to interact with the Feed.

BlogProxy service = new BlogProxy(name); //Create a new record BlogEntries b = new BlogEntries (); b.Title = "Blog Title 1"; b.Body = "This is a test blog entry"; service.AddObject("BlogEntries", b); service.SaveChanges(); int newRecordID = b.ID; //Select the newly added BlogEntry from the Feed var blog = service.BlogEntires.Where(blogPost=> blogPost.ID == newRecordID).FirstOrDefault(); //Select all records from the feed (up to page max) ordered by title var blogListing = service.BlogEntries.OrderBy(blog => blog.Title).ToList(); //Update the currently saved record blog.Title = "Updated Blog Title"; service.UpdateObject(blog); service.SaveChanges(); 

There are many other possible filters and search paramaters that can be applied when interacting with the feed. The great thing about the DataServiceContext class is it handles the LINQ to OData URL Translations for you so you can simply interact with your service like any .NET Proxy class.

Add a Comment



Log in with your Agility username and password or
register for the site

Log In