我有这暴露一个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">
能够获得这一信息$metadata
在C#aplication?
我有一个参考服务工作,和一些代码我使用了一个应用程序:
uriString中=的String.Format( “?PAssetsDepreciationBook $滤波器= FA_No当量 '{0}'”,cliente);
CONTEX =新ServiceReference1.NAV(新URI(serviceEndPoint)); contex.Credentials = CredentialCache.DefaultCredentials;
VAR客户= contex.Execute(新URI(uriString中,UriKind.Relative));
的foreach(在客户变种c)中{结果=结果+ c.Acquisition_Cost; }返回结果;
这工作得很好,但要获得$metadata
没有。
已经有代码(大部分)做到这一点。 它涉及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);
在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));
}
可以请求元数据作为使用纯XML HttpWebRequest
例如。 如果你需要分析它,你可以使用EdmLib
( Microsoft.Data.Edm.dll
),其上可以找到NuGet
甚至更好ODataLib
( Microsoft.Data.OData.dll
)还Nuget
具有ODataMessageReader
。 ReadMetadataDocument
专门用来阅读这些(它仍返回EDM对象模型,它正好与涉及EDMX
包装和版本你)。
谢谢,它工作得很好。
像这样:
// 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文件中的数据。