Skip to content

Instantly share code, notes, and snippets.

@markdstafford
Created July 11, 2012 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markdstafford/3092592 to your computer and use it in GitHub Desktop.
Save markdstafford/3092592 to your computer and use it in GitHub Desktop.
Sample JSON Light client
using System;
using System.Collections.Generic;
using System.Data.Services.Client;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.Data.Edm;
using Microsoft.Data.Edm.Csdl;
using Microsoft.Data.Edm.Validation;
using Scratch.NW;
namespace Scratch
{
internal class Program
{
private static readonly Dictionary<Uri, IEdmModel> models = new Dictionary<Uri, IEdmModel>();
private static void Main()
{
var context = new NorthwindEntities(new Uri("http://services.odata.org/Experimental/Northwind/Northwind.svc/"));
// Note that for the release, code gen will likely provide a much friendlier alternative to this example
context.Format.UseJson(args =>
{
Uri metadataUri = args.MetadataDocumentUri;
if (!models.ContainsKey(metadataUri))
{
var xmlTextReader = new XmlTextReader(metadataUri.ToString());
IEdmModel edmModel = null;
IEnumerable<EdmError> errors = null;
if (EdmxReader.TryParse(xmlTextReader, out edmModel, out errors))
{
models[metadataUri] = edmModel;
}
else
{
var errorMessageBuilder = new StringBuilder("Model creation failed; please resolve the following errors in the metadata document:");
foreach (EdmError error in errors)
{
errorMessageBuilder.AppendLine(String.Format("\t{0}", error.ErrorMessage));
}
throw new Exception(errorMessageBuilder.ToString());
}
}
return new ModelResolverResult
{
ServiceModel = models[metadataUri]
};
});
Category category = context.Categories.First();
Console.WriteLine(category.CategoryName);
foreach (Sales_Totals_by_Amount item in context.Sales_Totals_by_Amounts)
{
Console.WriteLine(item.SaleAmount);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment