我正在尝试为服务生成OData代理:http://services.odata.org/Northwind/Northwind.svc/ $ metadata
我正在使用System.Data.Services.Design.EntityClassGenerator来生成OData代理。
当我实例化EntityClassGenerator并调用GenerateCode时,输出没有错误。但是生成的代理代码中没有代码。
相同的代码适用于我自己的服务。但是,当我将它指向任何外部服务时,EntityClassGenerator无法正常工作。
以下是代码:
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(metadataEndpoint);
webRequest.Method = "GET";
webRequest.ContentType = "text/xml;encoding='utf-8";
webRequest.Proxy = (proxy != null) ? proxy : WebRequest.DefaultWebProxy;
using (WebResponse response = webRequest.GetResponse())
{
string xml = string.Empty;
XmlReaderSettings settings = new XmlReaderSettings();
using (TextReader reader = new StreamReader(response.GetResponseStream()))
{
xml = reader.ReadToEnd();
using (XmlTextReader sourceReader = new XmlTextReader(reader))
{
using (StringWriter targetWriter = new StringWriter())
{
// Generate the OData End point proxy.
EntityClassGenerator entityGenerator = new EntityClassGenerator(LanguageOption.GenerateCSharpCode);
entityGenerator.OnPropertyGenerated += new EventHandler<PropertyGeneratedEventArgs>(entityGenerator_OnPropertyGenerated);
IList<System.Data.Metadata.Edm.EdmSchemaError> errors = entityGenerator.GenerateCode(sourceReader, targetWriter, namespacename);
entityGenerator.OnPropertyGenerated -= new EventHandler<PropertyGeneratedEventArgs>(entityGenerator_OnPropertyGenerated);
odataProxyCode = targetWriter.ToString();
}
}
}
}
答案 0 :(得分:2)
我发现问题中的代码是完成OP所要求的有用起点。因此,即使OP不接受答案,我也会描述我为使其工作而做出的改变,以防其对其他人有用。
xml = reader.ReadToEnd();
来电。我假设这是为了调试目的来查看来自Web请求的响应,但它的结果是“清空”响应的reader对象。这意味着读者没有留下GenerateCode
电话。EntityClassGenerator
的使用更改为System.Data.Services.Design.EntityClassGenerator
。在下面的代码中,为了清晰和特殊,我包括整个名称空间。根据问题中的代码,OP似乎可能正在使用System.Data.Entity.Design.EntityClassGenerator
。我使用.NET Reflector来检查datasvcutil.exe
,这是一个可以生成代理类的命令行实用程序。我看到它引用了其他名称空间中的生成器。GenerateCode
电话中删除了错误。可以在调试器中检查它们,但无论如何都需要对它们进行某种自动检查。以下是我最终的结果:
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.
Create("http://services.odata.org/Northwind/Northwind.svc/$metadata");
webRequest.Method = "GET";
webRequest.ContentType = "text/xml;encoding='utf-8";
webRequest.Proxy = WebRequest.DefaultWebProxy;
using (WebResponse response = webRequest.GetResponse())
{
using (TextReader reader = new StreamReader(response.GetResponseStream()))
{
using (XmlTextReader sourceReader = new XmlTextReader(reader))
{
using (StringWriter targetWriter = new StringWriter())
{
// Generate the OData End point proxy.
System.Data.Services.Design.EntityClassGenerator entityGenerator =
new System.Data.Services.Design.EntityClassGenerator(
System.Data.Services.Design.LanguageOption.GenerateCSharpCode);
IList<System.Data.Metadata.Edm.EdmSchemaError> errors =
entityGenerator.GenerateCode(sourceReader, targetWriter,
"My.Model.Entities");
foreach (System.Data.Metadata.Edm.EdmSchemaError error in errors)
Console.WriteLine("{0}: {1}", error.Severity.ToString(), error.Message);
string odataProxyCode = targetWriter.ToString();
}
}
}
}