Share via


Azure OData and Windows Phone 7

In this post I am planning to cover the topic where we will be exposing data in SQL Azure using WCF Data Services and consume it from Windows Phone 7.

We have data available in SQL Azure. So we will create one let’s say and application ASP.NET MVC and add the Entity Framework data model there.

image

After we have added the model we will build the project and add one WCF Data Services file and modify the generated code as below,

 public class EmpDS : DataService<DBAzEntities>
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    }

    protected override DBAzEntities CreateDataSource()
    {
        DBAzEntities ctx = new DBAzEntities();
        ctx.ContextOptions.ProxyCreationEnabled = false;
        return ctx;
    }
}

After that upload it to the Azure Hosted services. Once that is done we now can view it in browser to check the validity of the URL. This would allow you to view your data in browser. After that we will build the Windows Phone 7 application to consume the data.

Build Windows Phone 7 Silverlight application and add “Service Reference” pointing to the URL

image

After that write the below code to MainPage.xaml

 public DataServiceCollection<Emp> Emps { get; set; } 

public void LoadData()
{
    DBAzEntities ctx = new DBAzEntities(new Uri(@"https://127.0.0.1:81/Models/EmpDS.svc/"));

    Emps = new DataServiceCollection<Emp>();
    var query = from em in ctx.Emps
                select em;
    Emps.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(Emps_LoadCompleted);
    Emps.LoadAsync(query);
}

void Emps_LoadCompleted(object sender, LoadCompletedEventArgs e)
{
    lstData.ItemsSource = Emps;
}

And the corresponding XAML would look like,

 <ListBox x:Name="lstData" >
    <ListBox.ItemTemplate>
        <DataTemplate>                    
            <StackPanel>
                <TextBlock Text="{Binding Path=FullName}"></TextBlock>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>            

And the final output would look like

image

Namoskar!!!