WCF Data Services for Windows Store Applications

WCF Data Services for Windows Store Applications

Rate This
  • Comments 1

The new version of WCF Data Services 5.6.0 is released since August 27 and now you will be able to make requests from a Windows store apps using both XML and JSON format.

As I said previously in my “Data Access : N Tiers Data Considerations”, making a JSON query with WCF Data Services from a Windows Store Apps was a little bit tricky …

Today, you can simply specify the format using the method UseJson() from the Format property of your DataServiceContext instance.
Pretty Straight forward !

Here is a short tutorial.

The source code of the tutorial : WCF Data Services Sample.zip

Installation

As said in the release post from Astoria Team, you need to :

Install the Tooling pack

Installing this tooling pack will ensure that Visual Studio will generate the correct proxy on the client, and the correct implementation of the server part.

        

image

WCF Data Services 5.6.0 Tools

Add the Nuget package

Adding the Nuget package will add the corrects assemblies in your server project and in your client project. Those assemblies will be published with your package.

image

Create a simple project

My project (code source : WCF Data Services Sample.zip) is very simple :

  1. Server side : Server.WcfDataServices
  2. Client side : Client.WcfDataServices

Server Side

On the server side, after installing WCF Data Services and Entity Framework 5, you need to

  1. Add your entity model (called PayNotes.edmx in my sample)
  2. Add your WCF Data Service proxy (called PayNotesServices.svc in my sample)

 

image

My entity model PayNotes.edmx

image

My WCF Data Service proxy PayNotesServices.svc

After a short compilation, you may be able to browse your OData service :

imagehttp://localhost:29285/PayNotesServices.svc/

imagehttp://localhost:29285/PayNotesServices.svc/Items

Remember that you can browse the $medata url to check the schema :)

imagehttp://localhost:29285/PayNotesServices.svc/$metadata

If you want test the JSON format, you can use Tools like Fiddler and specify the Accept property of your header :

image

With WCF Data Services 5.6.0, you can use several versions of the Accept header property :

application/atom+xml;type=feed,
application/atom+xml,
application/json;odata=minimalmetadata;streaming=true,
application/json;odata=minimalmetadata;streaming=false,
application/json;odata=minimalmetadata,
application/json;odata=fullmetadata;streaming=true,
application/json;odata=fullmetadata;streaming=false,
application/json;odata=fullmetadata,
application/json;odata=nometadata;streaming=true,
application/json;odata=nometadata;streaming=false,
application/json;odata=nometadata,
application/json;streaming=true,
application/json;streaming=false,
application/json;odata=verbose,
application/json

For example, here is the result with “application/json” :

image

Here is the result with “application/json;odata=fullmetadata;streaming=true” :

image

Here is the result with “application/json;odata=verbose” :

image

Client Side

My Client side project is a simple Windows Store Application project :

image

After installing the WCF Data Service client package, you need to add a Service Reference to your server side :

image

Then, you can start writing code :) In my sample project, you will find class called ContextModel, where you will find all the mandatory code :

First of all, you declare a new DataServiceContext instance, and you call the method UseJson() to make a Json HTTP call:

        private PayNotesEntities context;
        public ContextModel()
        {
            context = new PayNotesEntities(new Uri(ServiceUri));
            
            // Using JSON
            context.Format.UseJson();

            QueryOperationResponse<Item> results = (QueryOperationResponse<Item>)GetItems().Result;

            var lst = results.ToList();
        }

Here is the GetItems method:

        /// <summary>
        /// Using an extension method
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<Item>> GetItems()
        {
            DataServiceQuery<Item> query = (DataServiceQuery<Item>)(from item in context.Items
                                                                    select item);
            return await query.ExecuteAsync();
        }
Happy coding !
Attachment: WCF Data Services Sample.zip
Leave a Comment
  • Please add 4 and 8 and type the answer here:
  • Post
  • I installed this demo and when it runs, it shows the initial image then hangs.  When I compiled it, it updated the Nuget packages so they should eb current.  Any Ideas about what might not work here?  I am really trying hard to write an app for Microsoft Store and it seems that when I try to download the data asyncronously, it says that a list<of t> is not awaitable.  When I implement the BeginExecute and EndExecute, the results end up in the wrong thread.  Seems invoke isn't available.  

Page 1 of 1 (1 items)