public
Created

Sample JSON Light client

  • Download Gist
program.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
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);
}
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.