2
votes

I have a client application that is consuming a WCF Data Services OData service (both are v5.3.0). I'd like the client application to communicate with the service using JSON, instead of Atom Pub XML, which is the default.

Is this possible without without providing an IEdmModel instance? It's possible to do when using the Atom format:

        var ctx = new DataServiceContext(_oDataSvcUri, DataServiceProtocolVersion.V3)
        {
            IgnoreMissingProperties = true
        };

        // this isn't explicitly needed, as it uses Atom by default
        ctx.Format.UseAtom();

        return ctx;

Whereas for this to work using JSON, this is an example of what's required:

        var ctx = new DataServiceContext(_oDataSvcUri, DataServiceProtocolVersion.V3)
            {
                IgnoreMissingProperties = true
            };

        const string svcMetadata = "*insert contents of http://example.com/YourData.svc/$metadata here*";

        var xmlReader = XmlReader.Create(new StringReader(svcMetadata));

        IEdmModel edmModel = EdmxReader.Parse(xmlReader);

        ctx.Format.UseJson(edmModel);

        ctx.ResolveName = type => type.FullName;
        ctx.ResolveType = typeName => Type.GetType(typeName + ", " + "MyDomainModelAssemblyName");

        return ctx;

I'd like to be able to use the JSON format without specifying an IEdmModel like you can with Atom. Is this possible?

1

1 Answers

0
votes

It's not possible to read a JSON Light payload using the WCF Data Services client without having a server model client-side. JSON Light payloads are clean and small precisely because they assume that the client has an understanding of the server model and can compute all the extra metadata that's left out by using that.

However, you don't necessarily have to use EdmxReader to parse the server's $metadata document yourself. If you generated the client-side classes using "Add Service Reference" in Visual Studio and you use the derived DataServiceContext class (not DataServiceContext directly), the generated derived class has some configuration already set up to let you call ctx.Format.UseJson() without needing to explicitly provide a model.