Click here to Skip to main content
13,046,773 members (62,982 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


8 bookmarked
Posted 17 Sep 2010

Generic Approach to Access WCF Data Services

, 20 Sep 2010
Rate this:
Please Sign up or sign in to vote.
Generic approach to access WCF data services

WCF Data Services allow to publish your data very fast and easily. If you use Visual Studio to create the client which consumes the services, you just have to click "add service reference" and the classes you need to work with the service are generated (you will find a sample for WCF Data Service in my previous post). But what if you have a bunch of services which all follow 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 });

        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 as the source class, the data will be mapped to these properties. You also have 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)


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! 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mattia Baldinger
Software Developer
Switzerland Switzerland
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionIgnoreMissingProperties Pin
Anupama Roy8-Jan-11 18:24
memberAnupama Roy8-Jan-11 18:24 
QuestionWhat about Async ? Pin
RabinDl19-Sep-10 19:46
memberRabinDl19-Sep-10 19:46 
AnswerRe: What about Async ? Pin
Mattia Baldinger28-Sep-10 19:41
memberMattia Baldinger28-Sep-10 19:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 20 Sep 2010
Article Copyright 2010 by Mattia Baldinger
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid