错误:"无法从命名空间绑定的导入"错误消息出现,当您试图通过使用 Web 服务描述语言工具创建 XML Web 服务代理文件时

msdn:http://support.microsoft.com/kb/820122/zh-cn

核心就是把外部的参数移入一个wsdl中通过wsdl.exe生成

症状

您可以尝试使用 Web 服务描述语言工具 (Wsdl.exe) 创建 XML Web 服务代理文件用于 Web 服务描述语言 (WSDL) 文档。但是,如果 WSDL 文档使用的在使用包含元素 (<include>) 获取类型信息的外部 XML 架构 (.xsd 文件) 中定义的架构,您可能会收到以下错误消息:

错误: 无法导入来自命名空间 http://tempuri.org/ 绑定 Service1Soap。
-无法导入操作 HelloWorld。
-元素 http://tempuri.org/XMLSchema.xsd:element1 找不到。

Collapse image原因

您可能会收到 Wsdl.exe 无法获得有关在外部 XML 架构中声明的元素的完整信息时,"症状"一节中提到的错误。Wsdl.exe 无法处理包含元素 (<include>),除非包含元素都包含在类型部分 (<types>) 的 WSDL 文档中包含的架构元素 (<schema>)。因此,XML 架构包含有关外部 XML 架构中定义的元素的信息的使用来从外部的 XML 架构中获取其他信息包括元素时, 出现问题。

Collapse image替代方法

要变通解决此问题,您必须修改 WSDL 文档和 / 或将 XML 架构,以便不使用 XML 架构包含元素。若要这样做,请按照下列步骤操作:
  1. 打开 Service1.wsdl。将以下代码粘贴到类型元素 (<types>) 之后:

    注意这可以确保您使用的包含元素中而不是 XML 架构中的 Service1.wsdl
        <xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified"
          xmlns="http://tempuri.org/XMLSchema.xsd"
          xmlns:xs="http://www.w3.org/2001/XMLSchema">
          <xs:include schemaLocation="XMLSchema2.xsd" />
          <xs:element name="element1" type="MyTyp1"></xs:element>
        </xs:schema>
    Service1.wsdl now contains the following code:
    <?xml version="1.0" encoding="utf-8"?>
    <definitions xmlns:s1="http://tempuri.org/XMLSchema.xsd"
       xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
       xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
       xmlns:s="http://www.w3.org/2001/XMLSchema"
       xmlns:s0="http://tempuri.org/"
       xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
       xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"
       xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
       targetNamespace="http://tempuri.org/"
       xmlns="http://schemas.xmlsoap.org/wsdl/">
      <types>
        <xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified"
          xmlns="http://tempuri.org/XMLSchema.xsd"
          xmlns:xs="http://www.w3.org/2001/XMLSchema">
          <xs:include schemaLocation="XMLSchema2.xsd" />
          <xs:element name="element1" type="MyTyp1"></xs:element>
        </xs:schema>
        <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
          <s:import namespace="http://tempuri.org/XMLSchema.xsd" />
          <s:element name="HelloWorld">
            <s:complexType>
              <s:sequence>
                <s:element minOccurs="0" maxOccurs="1" ref="s1:element1" />
              </s:sequence>
            </s:complexType>
          </s:element>
          <s:element name="HelloWorldResponse">
            <s:complexType>
              <s:sequence>
                <s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" />
              </s:sequence>
            </s:complexType>
          </s:element>
        </s:schema>
      </types>
      <message name="HelloWorldSoapIn">
        <part name="parameters" element="s0:HelloWorld" />
      </message>
      <message name="HelloWorldSoapOut">
        <part name="parameters" element="s0:HelloWorldResponse" />
      </message>
      <portType name="Service1Soap">
        <operation name="HelloWorld">
          <input message="s0:HelloWorldSoapIn" />
          <output message="s0:HelloWorldSoapOut" />
        </operation>
      </portType>
      <portType name="Service1HttpGet" />
      <portType name="Service1HttpPost" />
      <binding name="Service1Soap" type="s0:Service1Soap">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
        <operation name="HelloWorld">
          <soap:operation soapAction="http://tempuri.org/HelloWorld" style="document" />
          <input>
            <soap:body use="literal" />
          </input>
          <output>
            <soap:body use="literal" />
          </output>
        </operation>
      </binding>
      <binding name="Service1HttpGet" type="s0:Service1HttpGet">
        <http:binding verb="GET" />
      </binding>
      <binding name="Service1HttpPost" type="s0:Service1HttpPost">
        <http:binding verb="POST" />
      </binding>
      <service name="Service1">
        <port name="Service1Soap" binding="s0:Service1Soap">
          <soap:address location="http://localhost/820122/New3/WebService1/Service1.asmx" />
        </port>
        <port name="Service1HttpGet" binding="s0:Service1HttpGet">
          <http:address location="http://localhost/820122/New3/WebService1/Service1.asmx" />
        </port>
        <port name="Service1HttpPost" binding="s0:Service1HttpPost">
          <http:address location="http://localhost/820122/New3/WebService1/Service1.asmx" />
        </port>
      </service>
    </definitions>
  2. 将移动到 Visual Studio.net 命令提示。更改为 Service1.wsdl 的目录路径。
  3. 从命令提示符处运行以下命令:

    wsdl Service1.wsdl

    注意在这种情况下不必提供 Wsdl.exe 与 XMLSchema1.xsd 作为命令行参数。您已经使用 Service1.wsdl 中的包含元素。

    您可能会收到一条消息,是类似于以下内容:

    写入文件 %File Path% \Service1.cs。


    %File Path%为 Service1.wsdl 的位置的占位符。

    此外,创建名为 Service1.cs XML Web 服务代理文件。
注意这些步骤基于此示例从本文"更多信息"一节。因此,代码和文件名称,在这些步骤可能与您的代码和您文件的名称不同。
posted @ 2014-03-26 09:12  feidaochuanqing  阅读(2392)  评论(0编辑  收藏  举报