Believe it or not, but we did ship ADO.NET Data Services with a few bugs.    One issue in particular is fairly nasty.   However the code developers might be tempted to write to work around the bug could cause far worse problems down the road when we fix the initial bug.  Hence I wanted to give a quick write up to describe the issue and give a quick sample of how to write code that will work now and in the future when we fix the issue.

The problem exists in the ADO.NET Data Services client (System.Data.Services.Client.dll) we shipped with .NET 3.5 sp1 and Silverlight 2.0 SDK.  In particular, we do not throw the correct exception when a HTTP request has problems while being sent to the server.  I.e. a request timeouts, there is a bad server name, etc.  In other words, any problem that would normally raise a System.Net.WebException.  Unfortunately, in V1 of ADO.NET Data Services a NullReferenceException is raised instead of a WebException and even worse, the exception message is lost:

DataServiceContext context = 
     new DataServiceContext(
         new Uri("http://BadServer/Northwind.svc"));
// Set small timeout amount to intentionally timeout request 
context.Timeout = 1; 
DataServiceQuery<Customer> query = 
     context.CreateQuery<Customer>("Customers");

try
{
    foreach (Customer c in query) { }
}
catch (NullReferenceException nre)
{
    // in V1, NullReferenceException is 
    //thrown instead of WebException.
    // Also, no useful description
    Console.WriteLine("Something bad happened!");
}

We are planning on fixing this bug in a future version of ADO.NET Data Services client (both in .NET and Silverlight) so if your code relies exclusively on catching the NullReference exception, it will be broken when we ship the fix.  Hence, to make your code robust – have your code explicitly catch both exception types:

DataServiceContext context = 
    new DataServiceContext(
        new Uri("http://BadServer/Northwind.svc"));
// Set small timeout amount to intentionally timeout request 
context.Timeout = 1;  
DataServiceQuery<Customer> query = 
    context.CreateQuery<Customer>("Customers");

try
{
    foreach (Customer c in query) { }
}
catch (WebException e)
{
    // When fix is shippped, will ADO.NET Data Services 
    // will correctly throw webException
    Console.WriteLine(e.Message);
}
catch (NullReferenceException nre)
{
    // in V1, NullReferenceException is thrown 
    // instead of WebException.
    // Also, no useful description
    Console.WriteLine("Something bad happened!");
}

We will update the team blog when we have a better idea of when the fix will be available.  All I can say now is we are looking into trying to get it out sooner than later, but we hope to have more information on this in the near future.

Andrew Conrad

ADO.NET Data Services Development Lead.