Got metadata in ODATA. what next?

2019-09-03 12:25发布

问题:

I am trying to fetch data from a service that i donot know much about.

So i got its url like http://ABC.com/ABC.svc

so i thouhgt to get metadata as http://ABC.com/ABC.svc/$metadata

it gives me:

<EntityType Name="E1">
- <Key>
<PropertyRef Name="E1k1" /> 
</Key>
< Property Name="E2" Type="Edm.String" Nullable="true"
 m:FC_TargetPath="SyndicationTitle" ..>


<ComplexType Name="OptionV1">
<Property Name="Value" Type="Edm.Int32" Nullable="true" /> 

... and a lot more.

How do i find out what should come next to ABC.svc/???

I want to write queries to access data. Can smebody point me to what should be my next steps? and any learning resource on this query generation from metadata would be hlpful.

Thanks

回答1:

There are two ways:

1) Using the service document. Navigate to the ABC.svc, that should return a service document, that is an ATOM Service payload which contains the names of the entity sets available from the service. For a sample of such you can go to http://services.odata.org/OData/OData.svc/. This should return a document with three collections (Entity sets). The href attribute is a relative URI to the entity set (relative to the xml:base which is usually the base of the service). So if for example your service has an entity set E1Set, then typically the address of it would be ABC.svc/E1Set.

2) Using the $metadata document and assuming the usual addressing scheme (note that this usually applies to the service but it doesn't have to). The $metadata document will define entity sets. Each of these is usually exposed by the service and typically follows the addressing scheme of ABC.svc/EntitySetName.

Once you navigate to the entity set, you should get back an ATOM feed with the entities in that set. The $metadata will help you recognize the shapes of the entities and the relationships.

Some services also have service operations or actions and so on. These are not exposed in the service document #1. Instead they are only visible in the $metadata as FunctionImport elements. They usually follow the addressing scheme of ABC.svc/FunctionImportName. But note that you might need to know something more about the service operation to be able to invoke it (what HTTP verb to use, what are the parameters, what it will do, and so on).



回答2:

LinqPad provides a very simple means for getting started with OData services (assuming some familiarity with LINQ). If you will primarily be primarily consuming this application from .NET, I'd recommend starting with this application. You point it to the $metadata endpoint and it generates proxy classes which allow you to work with the OData service much like you would in a plain-old-.NET-app. On the Results Log tab, it will output the URL used to query the OData service, which you can then pick up and tweak in Fiddler. (For more about how to use OData + Fiddler, see this blog post.)

If you'll primarily be using the OData service from JavaScript, you might want to start by understanding the URI conventions better or by playing around with data.js.