ODATA WebService,获取$元数据 C#

use*_*646 2 c# metadata odata

我有一个 WebService 公开了这个$metadata

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" Version="1.0">     > <edmx:DataServices m:DataServiceVersion="1.0"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">

<Schema xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns="http://schemas.microsoft.com/ado/2007/05/edm"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
Namespace="NAV"> 
<EntityType Name="PAsset"> 
<Key> <PropertyRef Name="No"/> </Key>  <Property Name="No" Nullable="false" Type="Edm.String"/>  <Property Name="Description"
Nullable="true" Type="Edm.String"/>  <Property Name="Inactive"
Nullable="false" Type="Edm.Boolean"/>  <Property Name="Insured"
Nullable="false" Type="Edm.Boolean"/> </EntityType> 

<EntityType Name="PAssetsDepreciationBook">

<EntityType Name="PBankAPostGrp">
<EntityType Name="PCompany">
<EntityType Name="PCustomer">
Run Code Online (Sandbox Code Playgroud)

$metadata是否可以在 C# 应用程序中获取其中的信息?

我有一个应用程序,其中引用了正在运行的服务,以及我正在使用的一些代码:

uriString = String.Format("PAssetsDepreciationBook?$filter=FA_No eq '{0}'",cliente);

contex = new ServiceReference1.NAV(new Uri(serviceEndPoint)); contex.Credentials = CredentialCache.DefaultCredentials;

var 客户 = contex.Execute(new Uri(uriString, UriKind.Relative));

foreach (var c incustomers) { 结果 = 结果 + c.Acquisition_Cost; 返回结果;

这很好用,但 get 却$metadata不行。

mhe*_*man 5

已经有代码(大部分)可以做到这一点。它涉及ODataMessageReader.ReadMetadataDocument()调用:

var request = WebRequest.CreateHttp(baseUrl + "$metadata");
var metadataMessage =
    new ClientHttpResponseMessage((HttpWebResponse)request.GetResponse());
using (var messageReader = new ODataMessageReader(metadataMessage))
{
    IEdmModel edmModel = messageReader.ReadMetadataDocument();
    // Do stuff with edmModel here
}
Run Code Online (Sandbox Code Playgroud)

您需要 ClientHttpResponseMessage 类,但这很简单(来自ODataLib101):

public class ClientHttpResponseMessage : IODataResponseMessage
{
    private readonly HttpWebResponse webResponse;

    public ClientHttpResponseMessage(HttpWebResponse webResponse)
    {
        if (webResponse == null)
            throw new ArgumentNullException("webResponse");
        this.webResponse = webResponse;
    }

    public IEnumerable<KeyValuePair<string, string>> Headers
    {
        get
        {
            return this.webResponse.Headers.AllKeys.Select(
                headerName => new KeyValuePair<string, string>(
                   headerName, this.webResponse.Headers.Get(headerName)));
        }
    }

    public int StatusCode
    {
        get { return (int)this.webResponse.StatusCode; }
        set
        {
            throw new InvalidOperationException(
                "The HTTP response is read-only, status code can't be modified on it.");
        }
    }

    public Stream GetStream()
    {
        return this.webResponse.GetResponseStream();
    }

    public string GetHeader(string headerName)
    {
        if (headerName == null)
            throw new ArgumentNullException("headerName");
        return this.webResponse.Headers.Get(headerName);
    }

    public void SetHeader(string headerName, string headerValue)
    {
        throw new InvalidOperationException(
            "The HTTP response is read-only, headers can't be modified on it.");
    }
}
Run Code Online (Sandbox Code Playgroud)

使用 IEdmModel,您可以访问元数据中的所有信息来执行诸如构建类型->控制器名称字典之类的操作:

Dictionary<type, string> typeControllerMap =
    edmModel.SchemaElements.OfType<IEdmEntityContainer>()
            .Single()
            .Elements.OfType<IEdmEntitySet>()
            .Select(es => new { t = FindType(es.ElementType.FullName()), n = es.Name })
            .Where(tn => tn.t != null)
            .ToDictionary(tn => tn.t, tn => tn.n);
Run Code Online (Sandbox Code Playgroud)

上面 LINQ 链中间的 FindType() 调用只是在所有程序集中搜索给定类型名称的类型

private static Type FindType(string fullName)
{
    return
        AppDomain.CurrentDomain.GetAssemblies()
            .Where(a => !a.IsDynamic)
            .SelectMany(a => a.GetTypes())
            .FirstOrDefault(t => t.FullName.Equals(fullName));
}
Run Code Online (Sandbox Code Playgroud)