WCF Data Services for Windows Store Applications
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.
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.
Create a simple project
My project (code source : WCF Data Services Sample.zip) is very simple :
- Server side : Server.WcfDataServices
- Client side : Client.WcfDataServices
Server Side
On the server side, after installing WCF Data Services and Entity Framework 5, you need to
- Add your entity model (called PayNotes.edmx in my sample)
- Add your WCF Data Service proxy (called PayNotesServices.svc in my sample)
My entity model PayNotes.edmx
My WCF Data Service proxy PayNotesServices.svc
After a short compilation, you may be able to browse your OData service :
https://localhost:29285/PayNotesServices.svc/
https://localhost:29285/PayNotesServices.svc/Items
Remember that you can browse the $medata url to check the schema :)
https://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 :
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” :
Here is the result with “application/json;odata=fullmetadata;streaming=true” :
Here is the result with “application/json;odata=verbose” :
Client Side
My Client side project is a simple Windows Store Application project :
After installing the WCF Data Service client package, you need to add a Service Reference to your server side :
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 !
Comments
- Anonymous
October 03, 2013
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.