如何为 Web 服务声明复杂的嵌套 C# 类型

发布于 2024-08-09 04:25:42 字数 1499 浏览 2 评论 0原文

我想创建一个接受复杂嵌套类型的服务。在我创建的示例 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓦然回首 2024-08-16 04:25:42

但这些元素空。您需要在它们出现之前构建它们,否则它们就是空的。

But those elements ARE null. You need to construct them before they show up otherwise they are just null.

甜柠檬 2024-08-16 04:25:42

正如 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.

未蓝澄海的烟 2024-08-16 04:25:42

.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 to true 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 a null value. Since it's an int, this additional flag is necessary to indicate whether or not to serialize this optional value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文