如何为 Web 服务声明复杂的嵌套 C# 类型
我想创建一个接受复杂嵌套类型的服务。在我创建的示例 asmx 文件中:
[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public class ServiceNest : System.Web.Services.WebService { public class Block { [XmlElement(IsNullable = false)] public int number; } public class Cell { [XmlElement(IsNullable = false)] public Block block; } public class Head { [XmlElement(IsNullable = false)] public Cell cell; } public class Nest { public Head head; } [WebMethod] public void TakeNest(Nest nest) { } }
当我在 IE 中查看 asmx 文件时,测试页面将示例 SOAP post 请求显示为:
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <TakeNest xmlns="http://schemas.intellicorp.com/livecompare/"> <nest> <head> <cell> <block xsi:nil="true" /> </cell> </head> </nest> </TakeNest> </soap:Body> </soap:Envelope>
它没有展开
查看 WSDL,所有类型看起来都不错。那么这只是帖子演示页面创建者的限制吗?
谢谢。
I would like to create a service that accepts a complex nested type. In a sample asmx file I created:
[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public class ServiceNest : System.Web.Services.WebService { public class Block { [XmlElement(IsNullable = false)] public int number; } public class Cell { [XmlElement(IsNullable = false)] public Block block; } public class Head { [XmlElement(IsNullable = false)] public Cell cell; } public class Nest { public Head head; } [WebMethod] public void TakeNest(Nest nest) { } }
When I view the asmx file in IE the test page shows the example SOAP post request as:
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <TakeNest xmlns="http://schemas.intellicorp.com/livecompare/"> <nest> <head> <cell> <block xsi:nil="true" /> </cell> </head> </nest> </TakeNest> </soap:Body> </soap:Envelope>
It hasn't expanded the <block> into its number member.
Looking at the WSDL, the types all look good. So is this just a limitation of the post demo page creator?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
但这些元素是空。您需要在它们出现之前构建它们,否则它们就是空的。
But those elements ARE null. You need to construct them before they show up otherwise they are just null.
正如 Kevin 指出的,示例 POST XML 表明这些元素为零。我应该只是尝试使用网络服务。完成此操作后,我可以看到导入器(.NET、Java 或 Ruby)正确创建了所有类型。所以这里确实没有任何问题。
As Kevin pointed out the example POST XML indicates that those elements are nil. I should have simply tried to consume the web service. Once I did that I could see that the importer (either .NET, Java or Ruby) correctly created all the types. So really there is no question here after all.
.NET 代码在达到一定级别后并没有放弃。
如果您查看“添加 Web 引用”生成的代码,您会发现有一个
bool numberSpecified
字段。仅当客户端将其设置为true
时,该数字才会被序列化。如果您查看 XML 架构,您会发现
number
元素可能不存在。如果它是引用类型,则可以在客户端中用null
值表示。由于它是一个int
,因此需要这个附加标志来指示是否序列化此可选值。The .NET code did not give up after a certain number of levels.
If you look at the code generated by "Add Web Reference", you'll find that there's a
bool numberSpecified
field. Only if the client sets that totrue
will the number be serialized.If you look at the XML Schema, you'll see that the
number
element might be absent. If it were of a reference type, then that could be represented in the client by anull
value. Since it's anint
, this additional flag is necessary to indicate whether or not to serialize this optional value.