Generic approach to access WCF Data Services

WCF Data Services allows to publish your data very fast and easy. If you use Visual Studio to create the client which consumes the services, you just can have to click “add service reference” and you have the classes you need to work with the service are generated (a sample for WCF Data Service you will find in my previous post). But what if you have a bunch of services which all follows the same convention and you want to access them in a generic way? This is also as simple as accessing the service with the generated stub. As a sample service we use one which returns all the startup date and time of the service (uses EF CT4 which can be downloaded here)

public class ServerInfo
{
    public int ID { get; set; }
    public DateTime Startup { get; set; }
}

public class ServerContext : DbContext
{
    public DbSet ServerInfos { get; set; }
}

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class ServerInfoService : DataService<ServerContext> 
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        ServerContext conn = new ServerContext();
        conn.ServerInfos.Add(new ServerInfo() { Startup = DateTime.Now });
        conn.SaveChanges();       
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
    }
}

All you need to access the service is the URL (of the service and the data). So you can use the base classes to call the service. But you still need a data container. WCF Data Services supports custom class binding, which means if a class contains the properties with the same name than the source class, the data will be mapped to these property. You have also the possibility to configure that missing properties in the destination class will be ignored.

public class ServerInfo
{
    public DateTime Startup { get; set; }
}

static void Main(string[] args)
{
    var serveruri = new Uri("http://localhost:7640/ServerInfoService.svc");
    var datauri = new Uri("/ServerInfos", UriKind.Relative);

    var context = new DataServiceContext(serveruri);
    context.IgnoreMissingProperties = true;

    var infos = context.Execute<ServerInfo>(datauri);

    foreach (var values in infos)
    {
        Console.WriteLine(values.Startup);
    }

    Console.ReadLine(); 
}

The DataServiceContext replaces your custom context class. To load the collection of ServerInfo objects, which normally would be accessed over a generated property, the generic method execute will be called. The data will be mapped automatically to the new class. When the property “IgnoreMissingProperties” is set to true, the mapping will also cause no failure if a property is missing!

Download the source code

16 thoughts on “Generic approach to access WCF Data Services

  1. lol a couple of the reviews bloggers write are just silly and unrelated, sometimes i wonder whether they at all read the post before writing or whether they merely look at the subject of the post and write the very first thought that comes to their minds. But it is nice to find a fresh commentary every now and then in contrast to the exact same, traditional blog garbage which I oftentimes notice on the blogs. Cheers

  2. I discovered your blog on google and skim a number of of your other posts. I simply added you to my Google News Reader. Keep up the great work. Stay up for studying more from you within the future.

  3. lol a couple of the reviews bloggers write are just silly and unrelated, sometimes i wonder whether they at all read the post before writing or whether they merely look at the subject of the post and write the very first thought that comes to their minds. But it is nice to find a fresh commentary every now and then in contrast to the exact same, traditional blog garbage which I oftentimes notice on the blogs. Cheers

  4. okfkgakmaokavsropphrcrlhh <a href="http://www.topgeldzaken.nl/lenen-zonder-bkr-toetsing/">lenen zonder bkr</a> fffafdlaade qnqvcphpqfgprtstnrhorkp <a href="http://www.topgeldzaken.nl/lenen-zonder-bkr-toetsing/">lenen zonder bkr toetsing</a> jekeeadefk nqtbghgnboaodihhvjpslhoop <a href="http://www.topgeldzaken.nl/lenen-zonder-bkr-toetsing/">lenen</a&gt; fegjfgked loaoklkahpervcaiklaaicpv <a href="http://www.topgeldzaken.nl/hypotheek-rente-vergelijken/">hypotheek</a&gt; jgajgaljeb hvfspafkkgehmoetkcqlhbhk <a href="http://hoofdpijnmigraine.nl/">migraine</a&gt; fdfdlldea

  5. Interesting article and one which should be more widely known about in my view. Your level of detail is good and the clarity of writing is excellent. I have bookmarked it for you so that others will be able to see what you have to say.

Comments are closed.