OData: Generate the $metadata document from a IEdm

2019-08-27 19:13发布

问题:

I can generate an IEdmModel from a $metadata document with something like this:

HttpWebRequest request = HttpWebRequest.CreateHttp("http://services.odata.org/v3/odata/OData.svc/$metadata");
ClientHttpResponseMessage metadataMessage = new ClientHttpResponseMessage((HttpWebResponse)request.GetResponse());
using (var messageReader = new ODataMessageReader(metadataMessage))
{
    IEdmModel model = messageReader.ReadMetadataDocument();
}

Is there a way to do the reverse? Given a IEdmModel generate the metadata document?

回答1:

Yes, you can use odl writer to write the metadata from IEdmModel. Class ODataMessageWriter have function WriteMetadataDocument. Assuming you have IEdmModel model, you can use following code to write metadata to message.

using (var msgWriter = new ODataMessageWriter((IODataResponseMessage)message, writerSettings, model))
{
  msgWriter.WriteMetadataDocument();
}


标签: odata