使用svcutil从WSDL和XSD生成类时,名称空间错误

问题描述

我有一个用于创建Web服务客户端的第三方WSDL文件和XML模式。 总体而言,WSDL如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:deFinitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
                  xmlns:tns="http://sample.com/" 
                  xmlns:types="http://sample.com/types" 
                  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
                  xmlns:ns="http://smaple.com/types" 
                  name="Sample" 
                  targetNamespace="http://sample.com/">
    <wsdl:types>
        <xsd:schema targetNamespace="http://sample.com/types" 
                xmlns="http://sample.com/types">
            <xsd:include schemaLocation="Sample.xsd"/>
            <xsd:element name="testReq" type="TestRequestMessage">
            </xsd:element>
      <xsd:element name="testRes" type="TestResponseMessage">
      </xsd:element>
        </xsd:schema>
    </wsdl:types>
    <wsdl:message name="TestReqMessage">
        <wsdl:part name="testReq" element="types:testReq"/>
    </wsdl:message>
    <wsdl:message name="TestResMessage">
        <wsdl:part name="testRes" element="types:testRes"/>
    </wsdl:message>
    <wsdl:portType name="Test_portType">
        <wsdl:operation name="TestOp">
            <wsdl:input message="tns:TestReqMessage"/>
            <wsdl:output message="tns:TestResMessage"/>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="Test_Binding" type="tns:Test_portType">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="TestOp">
            <soap:operation soapAction="urn:#TestOp" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="Test_XMLService">
        <wsdl:port name="Test-port" binding="tns:Test_Binding">
            <soap:address location="http://dummy.sample.com/Test-port"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:deFinitions>

这是XML模式:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            xmlns="http://sample.com/types" 
            xmlns:altova="http://www.altova.com/xml-schema-extensions" 
            targetNamespace="http://sample.com/types" 
            elementFormDefault="qualified">
    <!-- -->
    <!--Root elements -->
    <!-- -->
    <xsd:element name="TestRequestMessageElement" type="TestRequestMessage">
        <xsd:annotation>
            <xsd:documentation>Request</xsd:documentation>
        </xsd:annotation>
    </xsd:element>
  <xsd:element name="TestResponseMessageElement" type="TestResponseMessage">
    <xsd:annotation>
      <xsd:documentation>Response</xsd:documentation>
    </xsd:annotation>
  </xsd:element>
    <!-- -->
    <!--Complex types -->
    <!-- -->
  <xsd:complexType name="TestResponseMessage">
    <xsd:sequence>
      <xsd:element name="el1" type="Element1">
      </xsd:element>
      <xsd:element name="el2" type="Element2">
      </xsd:element>
    </xsd:sequence>
  </xsd:complexType>
    <xsd:complexType name="TestRequestMessage">
        <xsd:sequence>
            <xsd:element name="el1" type="Element1">
            </xsd:element>
            <xsd:element name="el2" type="Element2">
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="Element2">
        <xsd:sequence>
            <xsd:element name="no1">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:integer">
                        <xsd:minInclusive value="0"/>
                        <xsd:maxInclusive value="9"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
            <xsd:element name="no2">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:integer">
                        <xsd:minInclusive value="0"/>
                        <xsd:maxInclusive value="99999999"/>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="Element1">
        <xsd:sequence>
            <xsd:element name="no3" minOccurs="0">
            </xsd:element>
            <xsd:element name="date1" type="xsd:dateTime" minOccurs="0">
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

我使用svcutil通过以下命令从WSDL文件生成类:svcutil Sample.wsdl Sample.xsd

我现在要在应用程序中做的是在将这些类型的对象实例传递到Web服务之前对其进行验证。我尝试通过使用XmlSerializer序列化这样的实例并使用XmlReader执行验证来做到这一点:

TestRequestMessage msg = new TestRequestMessage();
msg.el1 = new ...

using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(schemaName))
{
    XmlSchema schema = XmlSchema.Read(stream,SchemaValidationHandler);
    XmlSchemaSet schemas = new XmlSchemaSet();
    schemas.Add(schema);

    using (MemoryStream memStream = new MemoryStream())
    {
        XmlSerializer ser = new XmlSerializer(typeof(RequestMessage));
        ser.Serialize(memStream,msg);
        memStream.Position = 0;

        bool errors = false;

        XmlReaderSettings settings = new XmlReaderSettings();
        settings.Schemas = schemas;
        settings.ValidationType = ValidationType.Schema;
        settings.ValidationEventHandler += (o,e) => {
            // act on validation error
        };

        using (XmlReader reader = XmlReader.Create(memStream,settings)) {
            while (reader.Read()) {}
        }

现在的问题是,即使我没有根据架构限制设置所有必需的属性或设置一些无效的值,验证也不会失败。

仔细查看生成的类,我意识到根元素不在它们应有的命名空间中。

如果我运行xsd.exe Sample.xsd会很有趣,将生成的类放在正确的名称空间中,即与使用svcutil生成代码相比,XmlRootAttribute设置了正确的名称名称空间。

我还想做的是使用xsd.exe开关在svcutil命令中重用reference工具生成的类。但这导致错误提示找不到System.Runtime.dll

但是我想做的是使用svcutil创建包括必要类型类的客户端代码。我该怎么做呢?

非常感谢您的帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)