none
How to dynamically generate service operations for a WCF Data Service RRS feed

  • Question

  • Is it possible to dynamically register service operations for a WCF Data Service, in a similar way that entities can be dynamically exposed via a custom IDataServiceMetadataProvider?

    Sunday, May 27, 2012 6:42 PM

Answers

  • If you implement your own IDataServiceMetadataProvider (IDSMP) and IDataServiceQueryProvider (IDSQP), it is definitely possible to dynamically generate service operations. The IDSMP has TryResolveServiceOperation and ServiceOperations members which are used to populate the metadata for the service operations. The IDSQP has InvokeServiceOperation which is called when the service operation is invoked.

    The sample of this using the untyped provider from the provider toolkit can be found in the sample services code: http://www.odata.org/ecosystem#samplecode (download the "Sample Services"). If you open the OData.svc.cs it uses the untyped provider to define and then implement a service operation called GetProductsByRating.

    Thanks,


    Vitek Karas [MSFT]

    • Proposed as answer by Ashwini47 Monday, May 28, 2012 10:44 AM
    • Marked as answer by MTraudt Monday, May 28, 2012 11:53 AM
    Monday, May 28, 2012 8:59 AM
    Moderator

All replies

  • Hi 

    I am not very sure what is your exact requirement but there are couple of fact that i would like to share 

    1.  you can not implement only metadata provider , you will also need to implement at least query provider that won`t be a simple task so if possible then ignore that path. In general customization is trouble sum in data service . I have consolidated few points from my experience in my blog post WCF Data Service (OData) Best Practices ,Dos and Don`t dos 
    2. Service Operation are not very well supported by client libraries and it will be hard job for your end user to consume it http://msdn.microsoft.com/en-us/library/hh230677.aspx

    can you please share your detailed requirement ,may we could suggest some workaround  

    Thanks

    Ashwini

    Sunday, May 27, 2012 7:07 PM
  • Thanks for the quick response.  I had seen some of the referenced articles but not all of them.

    The plan was to implement both IDataServiceMetadataProvider and IDataServiceQueryProvider (using the UnTyped ODataProviderToolkit sample as a starting point). If that proves too difficult we will have to rethink this approach.

    In any case, our custom metadata provider is dynamically generating entity types based on metadata in the source system (the metadata can change while the system is running and so the entities cannot be hard-coded).

    For various reasons we wanted to implement service operations that return entities based on arguments provided by the caller.  However this would require the service operations to know about the (generated) entity types and so would mean (I think) that the service operations themselves must also be dynamically generated by the metadata provider.  This seemed to me to be a reasonable thing to want to do, but I do not see any way to define service operations other than using custom attributes on hard-coded methods of the service.

    Sunday, May 27, 2012 10:42 PM
  • If you implement your own IDataServiceMetadataProvider (IDSMP) and IDataServiceQueryProvider (IDSQP), it is definitely possible to dynamically generate service operations. The IDSMP has TryResolveServiceOperation and ServiceOperations members which are used to populate the metadata for the service operations. The IDSQP has InvokeServiceOperation which is called when the service operation is invoked.

    The sample of this using the untyped provider from the provider toolkit can be found in the sample services code: http://www.odata.org/ecosystem#samplecode (download the "Sample Services"). If you open the OData.svc.cs it uses the untyped provider to define and then implement a service operation called GetProductsByRating.

    Thanks,


    Vitek Karas [MSFT]

    • Proposed as answer by Ashwini47 Monday, May 28, 2012 10:44 AM
    • Marked as answer by MTraudt Monday, May 28, 2012 11:53 AM
    Monday, May 28, 2012 8:59 AM
    Moderator
  • Thanks - this is exactly what I was looking for!
    Monday, May 28, 2012 11:53 AM