Expand and Load: Fetching related entities in WCF Data Service

Objective

In this post, I will show you how to retrieve data from related entities in WCF Data Service.

If you are new to this topic, please read Introduction to WCF Data service and ODATA before going through below article

Let us say, we want to fetch details from related entities Customers and Order.

clip_image001

There are two ways to fetch data from both related entities.

1. Expand

2. LoadProperty

Using LoadProperty

Program.cs

  NorthwindEntities context = new NorthwindEntities(new Uri("http://localhost:61091/WcfDataService1.svc/"));
                try
                {
                    foreach (Order o in context.Orders)
                    {
                        context.LoadProperty(o, "Customer");
                        Console.WriteLine("Customer : {0}- Order Id: {1}", o.Customer.ContactName, o.OrderID);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.Read();
            }

In above code clip_image003 So in above code load property is being used to load data from related entities. Output clip_image005 Using Expand Program.cs

 DataServiceQuery<Order> query =
                context.Orders.Expand("Order_Details,Customer");
            try
            {
                foreach (Order order in query.Take(4))
                {
                    Console.WriteLine("Customer: {0}", order.Customer.ContactName);
                    Console.WriteLine("Order ID: {0}", order.OrderID);
                    foreach (Order_Detail item in order.Order_Details)
                    {
                        Console.WriteLine("\tProduct: {0} - Quantity: {1}",
                            item.ProductID, item.Quantity);
                    }
                }
            }
            catch (DataServiceQueryException ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();

So in above code

clip_image007

In above code Orders relation is expanded to Order_detail and Customer.

clip_image009

In above code retrieving top 4th record and iterating through the records to display.

Output

clip_image011

WebException or Remote server name could not be resolved in WCF Data Service

Objective

This article will give a brief explanation on how to handle remote server name could not resolved exception in WCF Data Service

If you are new to this topic, please read Introduction to WCF Data service and ODATA before going through below article

Let us say, there is a WCF Data Service up and running on a particular server. We access that using below code at the client side

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Services.Client;
using ConsoleApplication1.ServiceReference1;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataServiceContext context = new DataServiceContext(
                                         new Uri("http://localhost:61091/WcfDataService1.svc/"));
            DataServiceQuery<Employee> query = context.CreateQuery<Employee>("Employees");
                foreach (Employee e in query)
                {
                    Console.WriteLine(e.FirstName);
                }
            Console.Read();
        }
    }
}

And output is as below,

clip_image002

Every this is fine till this point. Now let us go ahead and modify the WCF Data Service URL to a false URL

clip_image004

And URL, I am giving is false one. Server abc does not exist. Now after modification code will look like

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Services.Client;
using ConsoleApplication1.ServiceReference1;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataServiceContext context = new DataServiceContext(
                                         new Uri("http://abc:61091/WcfDataService1.svc/"));
            DataServiceQuery<Employee> query = context.CreateQuery<Employee>("Employees");
                foreach (Employee e in query)
                {
                    Console.WriteLine(e.FirstName);
                }
            Console.Read();
        }
    }
}

And when you try to run, you will get the run time exception as below,

clip_image005

Now to handle this exception put the foreach statement in try catch , so no modified code will look like

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Services.Client;
using ConsoleApplication1.ServiceReference1;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataServiceContext context = new DataServiceContext(
                                         new Uri("http://abc:61091/WcfDataService1.svc/"));
            DataServiceQuery<Employee> query = context.CreateQuery<Employee>("Employees");
            try
            {
                foreach (Employee e in query)
                {
                    Console.WriteLine(e.FirstName);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
    }
}

And now running output will be as below,

clip_image007