locked
How to expose oData vocabularies with an typed and untyped WCF Custom Data Service Provider? RRS feed

  • Question

  • Is there an example for implementing oData vocabularies with an typed and untyped WCF Custom Data Service Provider?

    WCF Data Services 5.0 has support for oData vocabularies (http://blogs.msdn.com/b/astoriateam/archive/2011/10/13/vocabularies-in-wcf-data-services.aspx)however the example only seems to be relevant to when the data model is known at compile time.

    The Custom Data Service Providers (http://msdn.microsoft.com/en-us/data/gg191846) allow for a data model for late-bound data types. However I don't see a way to conveniently apply vocabularies to the metadata.

    One possibility would be to intercept the request for metadata and supply my version of the metadata marked up with the vocabularies. But it isn't clear to me how to accomplish that.

    So inconclusion: Is there an example for implementing oData vocabularies with an typed and untyped WCF Custom Data Service Provider? If not, is there I can intercept the metadata request and substitute my own metadata marked up with the vocabularies?

    Thanks.

    Saturday, November 17, 2012 7:46 PM

Answers

  • The same approach as in the linked blog post should work for custom dynamic providers as well. Your code will just need to generate the right annotation file for the given model. I agree it's not the easiest way to do it (better would be to be able to specify annotations while generating the model), but I don't see anything making it not work.

    Thanks,


    Vitek Karas [MSFT]

    • Marked as answer by Kilgore3000 Monday, November 26, 2012 10:23 PM
    • Unmarked as answer by Kilgore3000 Monday, November 26, 2012 10:24 PM
    • Marked as answer by Kilgore3000 Monday, November 26, 2012 10:26 PM
    Monday, November 19, 2012 8:54 AM
    Moderator

All replies

  • The same approach as in the linked blog post should work for custom dynamic providers as well. Your code will just need to generate the right annotation file for the given model. I agree it's not the easiest way to do it (better would be to be able to specify annotations while generating the model), but I don't see anything making it not work.

    Thanks,


    Vitek Karas [MSFT]

    • Marked as answer by Kilgore3000 Monday, November 26, 2012 10:23 PM
    • Unmarked as answer by Kilgore3000 Monday, November 26, 2012 10:24 PM
    • Marked as answer by Kilgore3000 Monday, November 26, 2012 10:26 PM
    Monday, November 19, 2012 8:54 AM
    Moderator
  • Thanks Vitek,

    Your response got me pointed in the right direction. I was able to successfully add the annotations to the metadata by modifying the ReadOnly example of the typed custom data provider.

    In case anyone else is curious. Here is the content of my annotations file:

    <?xml version="1.0"?>
    <Schema xmlns="http://schemas.microsoft.com/ado/2009/11/edm" Alias="VocabSample" Namespace="VocabSample"> <Using Alias="Validation" Namespace="MyValidationNamespace"/> <Annotations Target="DataServiceProviderDemo.Product/ID"> <TypeAnnotation Term="ID.Range"> <PropertyValue Decimal="1" Property="Min"/> <PropertyValue Decimal="1000" Property="Max"/> </TypeAnnotation> </Annotations> </Schema>

    Here is the revised InitializeService() method:

            public static void InitializeService(DataServiceConfiguration config)
            {
                config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
                config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
                config.DataServiceBehavior.AcceptProjectionRequests = true;

                //vocabulary support
                const string annotationsFile = @"C:\Annotations.xml";
                config.DisableValidationOnMetadataWrite = true;
                config.AnnotationsBuilder = (model) =>
                {
                    IEdmModel annotationsmodel;
                    IEnumerable<EdmError> errors;
                    var xmlreaders = new XmlReader[] { XmlReader.Create(annotationsFile) };
                    bool parsed = CsdlReader.TryParse(xmlreaders, model, out annotationsmodel, out errors);
                    return parsed ? new IEdmModel[] { annotationsmodel } : null;
                };

            }

    The annotated metadata can be viewed from the metadata endpoint, for example:
    http://localhost:1337/DemoDSPDataService.svc/$metadata

    Monday, November 26, 2012 10:27 PM