-
Create a WCF Data Service (OData) to share an Azure Table
1 CommentThe Open Data (OData) is a new protocol for querying and updating data. Find in this site a list of sites that are already supporting OData. Windows Azure Table Storage is one of them but to use this endpoint, the storage key is needed.
Sharing an Azure Table is easy using WCF Data Service and Azure SDK.
1. First, create the class that will be used by the ADO.NET Data Service to create the service definition. All the IQueryable properties in this class will become a collection shared in the service.
Here is the class that we will use in our service, called AzureTableContext. The only collection that will be exposed is the Menu (public IQueryable<MenuItemRow> Menu).
namespace Sample.OData
{
using System;
using System.Configuration;
using System.Data.Services.Common;
using System.Linq;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;public class AzureTableContext
{
private readonly TableServiceContext tableContext;public AzureTableContext()
{
CloudStorageAccount.SetConfigurationSettingPublisher(
(configName, configSetter) =>
configSetter(ConfigurationManager.AppSettings[configName].ToString()));var account = CloudStorageAccount.FromConfigurationSetting(“DataConnectionString”);
this.tableContext = new TableServiceContext(account.TableEndpoint.ToString(), account.Credentials);
}public IQueryable<MenuItemRow> Menu
{
get
{
return this.tableContext.CreateQuery<MenuItemRow>(“Menu”).AsTableServiceQuery();
}
}
}[EntityPropertyMapping("Name", SyndicationItemProperty.Title, SyndicationTextContentKind.Plaintext, true)]
public class MenuItemRow : TableServiceEntity
{
public string Name { get; set; }
public string Description { get; set; }
public DateTime CreatedOn { get; set; }
}
}The attribute EntityPropertyMapping has been added in order to have the name of the MenuItemRow displayed as Title when browsing the service from a web browser.
2. The Cloud Storage Account configuration is read from the web.config. Make sure that the following setting is in your site’s configuration:
Web.config file configuration
<appSettings>
<add key=”DataConnectionString” value=”UseDevelopmentStorage=true” />
</appSettings>3. Right-click an existing website and select “Add”> “New item”. On the top-right corner’s textbox, type: “WCF Data Service”.
4. Add the following code to the class that has been auto-generated by the wizard.
namespace Sample.OData
{
using System.Data.Services;
using System.Data.Services.Common;[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class WcfDataService1 : DataService<AzureTableContext>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
config.SetEntitySetAccessRule(“*”, EntitySetRights.AllRead);
config.SetServiceOperationAccessRule(“*”, ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
config.UseVerboseErrors = true;
}
}
}The ServiceBehavior attribute has been added to help debugging. It’s also very useful to set the UseVerboseErrors in the data service configuration to get better error messages.
The “*” in the SetEntitySetAccessRule and SetServiceOperationAccessRule configurations will allow querying all the entities.
5. Run the solution and browse to the service’s web page. The service definition is displayed.
Given we have exposed the Menu collection, we can browse it by adding Menu at the end of the service’s url.
All the rows in the Menu table are displayed as an atom feed.
-
1 Comment:
Leave a comment
Your email address will not be published.
Twitter Trackbacks for Blogged: Create a WCF Data Service (OData) to share an Azure Table [southworks.net] on Topsy.com said on July 29, 2010:
[...] Blogged: Create a WCF Data Service (OData) to share an Azure Table blogs.southworks.net/fboerr/2010/07/29/create-a-wcf-data-service-odata-to-share-an-azure-table/ – view page – cached Tweets about this link [...]