OData: Generate the $metadata document from a IEdm

2019-08-27 18:58发布

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?

标签: odata
1条回答
家丑人穷心不美
2楼-- · 2019-08-27 19:21

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();
}
查看更多
登录 后发表回答