2012-07-03 46 views
1

我有一個公開此$metadata一個WebService:

<?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"> 

可能獲得在C#aplication在此$metadata的信息?

我有到服務工作的引用的應用程序,以及一些代碼我使用:

uriString中=的String.Format(「PAssetsDepreciationBook $過濾器= FA_No EQ '{0}' 」,cliente);

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

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

foreach(客戶中的變量c) 結果=結果+ c.Acquisition_Cost; } 返回結果;

這工作正常,但得到$metadata沒有。

回答

1

例如,您可以使用HttpWebRequest作爲純XML請求元數據。如果需要解析它,可以使用EdmLibMicrosoft.Data.Edm.dll),它可以在NuGet或更好的ODataLibMicrosoft.Data.OData.dll)上找到,也可以在NugetODataMessageReader上找到。 ReadMetadataDocument專門設計用於讀取這些文件(它仍然會返回EDM對象模型,它只是爲您處理EDMX包裝和版本)。

1

謝謝,它工作得很好。

就像這樣:

// Create a request for the URL. 
WebRequest request = WebRequest.Create("http://localhost:7048/DynamicsNAV70/OData/$metadata"); 

//set the credentials. 
request.Credentials = CredentialCache.DefaultCredentials; 

// Get the response. 
WebResponse response = request.GetResponse(); 

// Get the stream containing content returned by the server. 
Stream dataStream = response.GetResponseStream(); 

// Open the stream using a StreamReader for easy access. 
StreamReader reader = new StreamReader(dataStream); 

// Read the content. 
string responseFromServer = reader.ReadToEnd(); 

XmlDocument xmlDoc = new XmlDocument(); 
xmlDoc.Load(new StringReader(responseFromServer)); 

// Clean up the streams and the response. 
reader.Close(); 
response.Close(); 

在此之後,只是像解析XML文件中的數據。

2

已經有代碼(主要是)要做到這一點。它涉及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 
} 

您需要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."); 
    } 
} 

隨着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); 

FindType()呼叫在LINQ鏈以上簡單的中間搜索所有組件給定的類型名稱的類型:

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