【问题标题】:Mapping WSDL enumeration to string in ASP.NET webservice将 WSDL 枚举映射到 ASP.NET Web 服务中的字符串
【发布时间】:2023-03-23 17:37:01
【问题描述】:

背景: 我正在使用 ASP.NET 2.0 创建 Web 服务。此 Web 服务为现有 Web 表单提供了另一个接口,其中包含从数据库动态填充的选择框。

我的 Web 服务初稿接受了每个字符串,然后确保它是有效的,如果不是则返回一个错误。然而,由于可能的值不太可能经常更改,Web 服务的使用者要求我们在 WSDL 中提供枚举值。

我不愿意使用我的 Web 服务代码创建枚举,因此我更改了生成的 WSDL 文件并指示我的 Web 服务使用它而不是检查我的类来生成它。

WSDL:

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://example.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://example.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  <wsdl:types>
    <s:schema elementFormDefault="qualified" targetNamespace="http://example.org/">
      <s:element name="MyMethod">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="myClass" type="tns:MyClass" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:complexType name="MyClass">
        <s:sequence>
          <s:element minOccurs="0" maxOccurs="1" name="MyString" type="tns:MyStringPossibleValues" />
        </s:sequence>
      </s:complexType>
      <s:element name="MyMethodResponse">
        <s:complexType />
      </s:element>
      <s:simpleType name="MyStringPossibleValues">
        <s:restriction base="s:string">
          <s:enumeration value="alpha" />
          <s:enumeration value="bravo" />
        </s:restriction>
      </s:simpleType>
    </s:schema>
  </wsdl:types>
  <wsdl:message name="MyMethodSoapIn">
    <wsdl:part name="parameters" element="tns:MyMethod" />
  </wsdl:message>
  <wsdl:message name="MyMethodSoapOut">
    <wsdl:part name="parameters" element="tns:MyMethodResponse" />
  </wsdl:message>
  <wsdl:portType name="ExternalAccessSoap">
    <wsdl:operation name="MyMethod">
      <wsdl:input message="tns:MyMethodSoapIn" />
      <wsdl:output message="tns:MyMethodSoapOut" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:portType name="ExternalAccessHttpGet" />
  <wsdl:portType name="ExternalAccessHttpPost" />
  <wsdl:binding name="ExternalAccessSoap" type="tns:ExternalAccessSoap">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="MyMethod">
      <soap:operation soapAction="http://example.org/MyMethod" style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:binding name="ExternalAccessSoap12" type="tns:ExternalAccessSoap">
    <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="MyMethod">
      <soap12:operation soapAction="http://example.org/MyMethod" style="document" />
      <wsdl:input>
        <soap12:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap12:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
</wsdl:definitions>

网络服务:

namespace Example.Service
{
    [WebService(Namespace = "http://example.org/")]
    [WebServiceBinding(
        ConformsTo = WsiProfiles.BasicProfile1_1,
        Location="ExternalAccess.wsdl",
        Name="ExternalAccessSoap",
        Namespace = "http://example.org/")]
    [ToolboxItem(false)]
    public class ExternalAccess : System.Web.Services.WebService
    {
        public class MyClass
        {
            public string MyString;
        }

        [WebMethod]
        [SoapDocumentMethod(
            Action = "http://example.org/MyMethod",
            RequestNamespace = "http://example.org/",
            Binding="ExternalAccessSoap")]
        public void MyMethod(MyClass myClass)
        {
        }
    }
}

问题: 由于 WSDL 指定了 MyString 的枚举并且代码指定了字符串类型,ASP.NET 无法正确映射字段。

是否有一个属性可以用来指示反序列化器使用枚举值填充字符串字段?

问候,

马特

【问题讨论】:

    标签: c# asp.net xml web-services wsdl


    【解决方案1】:

    在为我完成了创建一个soap扩展的过程后,我发现MyString实际上并没有被发送到我的网络服务。

    这是因为该服务的测试应用程序也是在 .NET 中构建的,并且在构建请求对象时,生成的代理类的 MyStringSpecified 属性被忽略了。这会阻止枚举值作为 SOAP 请求的一部分发送。

    当此属性设置为 true 时,枚举值已成功分配给 web 服务中的 MyString 字段。

    【讨论】:

      猜你喜欢
      • 2021-11-09
      • 2019-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 2013-04-05
      • 2011-04-14
      相关资源
      最近更新 更多