文章目录

第八十二章 方法关键字 - SoapTypeNameSpace

为此web方法使用的类型指定XML命名空间。仅适用于定义为web服务web客户端的类。

用法

若要重写类型的默认XML命名空间(当该方法用作web方法时),请使用以下语法:

Method name(formal_spec) As returnclass [ SoapTypeNameSpace = "soapnamespace", SoapBindingStyle = document, WebMethod ] 
{    //implementation }

其中soapnamespace是命名空间URI。请注意,如果URI包含冒号(:),则该字符串必须加引号。也就是说,可以使用以下内容:

Method MyMethod() [ SoapTypeNameSpace = "http://www.mynamespace.org", SoapBindingStyle = document, WebMethod ] 

或以下内容:

Method MyMethod() [ SoapTypeNameSpace = othervalue, SoapBindingStyle = document, WebMethod ] 

但不包括以下内容:

Method MyMethod() [ SoapTypeNameSpace = http://www.mynamespace.org, SoapBindingStyle = document, WebMethod ] 

重要提示:对于手动创建的web服务,该关键字的默认值通常是合适的。当使用SOAP向导从WSDL生成web客户端或服务时,InterSystems IRIS会将该关键字设置为适合该WSDL;如果修改该值,web客户端或服务可能不再工作。

详解

此关键字指定此web方法使用的类型的XML命名空间。

注意:只有当方法使用文档样式绑定时,此关键字才有作用。也就是说,方法(或包含它的类)必须用等于documentSoapBindingStyle标记。(对于使用rpc-style绑定的方法,指定这个关键字是没有意义的。)

默认

如果省略此关键字,则此方法的类型位于由web服务客户端类的TYPENAMESPACE参数指定的命名空间中。如果未指定TYPENAMESPACE,则类型将位于由web服务或客户端的are参数指定的命名空间中。

与WSDL的关系

SoapTypeNameSpace关键字影响WSDL的以下部分:

<definitions>元素中的命名空间声明。指定的命名空间(例如,http://www.customtypes.org)将添加到这里。例如:

...
xmlns:ns2="http://www.customtypes.org" 
xmlns:s0="http://www.wbns.org" 
xmlns:s1="http://webservicetypesns.org" 
...
targetNamespace="http://www.wbns.org"

在本例中,http://www.customtypes.org命名空间被分配给前缀ns2

请注意,WSDL还像往常一样声明了以下名称空间:

  • Web服务的命名空间(http://www.wsns.org),在本例中,它被分配给前缀s0,也用作Web服务的目标命名空间。
  • 网络服务的类型命名空间http://www.webservicetypesns.org),在本例中它被分配给前缀s1

如果在web服务类中没有指定类型命名空间,则该命名空间不包含在WSDL中。

  • <types>元素,它包含一个<schema>元素,该元素的targetNamespace属性等于为SoapTypeNameSpace指定的命名空间:
<types>
...
<s:schema elementFormDefault="qualified" targetNamespace="http://www.customtypes.org">
    <s:element name="Add">
        <s:complexType>
            <s:sequence>
                <s:element minOccurs="0" name="a" type="s:decimal"/>
                <s:element minOccurs="0" name="b" type="s:decimal"/>
            </s:sequence>
        </s:complexType>
    </s:element>
    <s:element name="AddResponse">
        <s:complexType>
            <s:sequence>
                <s:element name="AddResult" type="s:decimal"/>
            </s:sequence>
        </s:complexType>
    </s:element>
</s:schema>

...
</types>

相反,如果没有指定SoapTypeNameSpace,那么WSDL的这一部分将如下所示。请注意,<schema>元素的targetNamespaceweb服务类型的命名空间:

<types>
...
<s:schema elementFormDefault="qualified" targetNamespace="http://www.webservicetypesns.org">
    <s:element name="Add">
        <s:complexType>
            <s:sequence>
                <s:element minOccurs="0" name="a" type="s:decimal"/>
                <s:element minOccurs="0" name="b" type="s:decimal"/>
            </s:sequence>
        </s:complexType>
    </s:element>
    <s:element name="AddResponse">
        <s:complexType>
            <s:sequence>
                <s:element name="AddResult" type="s:decimal"/>
            </s:sequence>
        </s:complexType>
    </s:element>
</s:schema>

...
</types>

(此外,如果在web服务类中没有指定类型命名空间,则targetNamespace将改为web服务的命名空间。)

对消息的影响

SOAP消息可能如下所示(为了可读性,添加了换行符和空格):

<?xml version="1.0" encoding="UTF-8" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' 
                   xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
                   xmlns:s='http://www.w3.org/2001/XMLSchema'>
   <SOAP-ENV:Body>
      <AddResponse xmlns="http://www.customtypes.org">
         <AddResult>3</AddResult>
      </AddResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

请注意,<AddResponse>元素位于“http://www.customtypes.org”命名空间中。

相反,如果没有指定SoapTypeNameSpace关键字,则消息可以如下所示:

<?xml version="1.0" encoding="UTF-8" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' 
                   xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
                   xmlns:s='http://www.w3.org/2001/XMLSchema'>
   <SOAP-ENV:Body>
      <AddResponse xmlns="http://www.webservicetypesns.org">
         <AddResult>3</AddResult>
      </AddResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>