代码之家  ›  专栏  ›  技术社区  ›  Shrike

使用IXmlSerializable导出数据协定类型的WSDL/XSD模式

  •  0
  • Shrike  · 技术社区  · 15 年前

    问题是当我们通过mex端点获取服务的wsdl时( http://..svc?wsdl )不会返回该架构。

    以下是详细情况。

     [ServiceContract]
     public interface IService1
     {
      [OperationContract]
      DomainData GetData();
     }
    

    域数据类型为:

     [DataContract(Namespace = "http://schemas.biz.org/Samples/customserialization")]
     public class DomainData
     {
      [DataMember(Name = "AuxData")]
      Dictionary<String, AuxDomainData> m_auxData = new Dictionary<string, AuxDomainData>();
    
      [DataMember]
      public string ObjectId { get; set; }
    
      public IDictionary<string, AuxDomainData> AuxData
      {
       get { return m_auxData; }
      }
     }
    

     [XmlSchemaProvider("GetXmlSerializationSchema")]
     public class AuxDomainData : IXmlSerializable
     {
      [DataMember]
      public Object AuxData { get; set; }
    
      XmlSchema IXmlSerializable.GetSchema() { return null; }
    
      void IXmlSerializable.ReadXml(XmlReader reader) { }
    
      void IXmlSerializable.WriteXml(XmlWriter writer) { }
    
      public static string Namespace = "http://schemas.biz.org/Samples/customserialization";
    
      public static XmlQualifiedName GetXmlSerializationSchema(XmlSchemaSet schemas)
      {
       var qname =  new XmlQualifiedName("AuxDomainData", Namespace);
       string resourceName = "CustomSerialization.aux-domain-data.xsd";
       using (Stream stream = typeof(AuxDomainData).Assembly.GetManifestResourceStream(resourceName))
       {
        var schema = XmlSchema.Read(stream, null);
        schemas.Add(schema);
       }
       return qname;
      }
     }
    

    这里,我们将在GetXmlSerializationSchema方法中返回XSD模式。 模式本身很简单,但让我跳过这里。

    我想代码很简单,这是IXmlSerializable类型的常见场景。

    现在,我们需要WSDL。我将在的帮助下使用WSDL创建Java客户机 Metro 但实际上JDK1.6已经足够了,因为它包含WS-stack(和wsimport.exe)。 所以java需要wsdl和wsdl:服务定义。这就是为什么我不能从wsdl.exe给它一个wsdl(因为wsdl生成的wsdl不包含wsdl:service定义,只包含wsdl:portType)。 所以,我叫wsimport.bat http://localhost/Service1.svc?wsdl

    [错误]未定义的简单或复杂类型“q1:AuxDomainData” http://locahost/CustomSerialization/Service1.svc?xsd=xsd3

    <wsdl:types>
      <xsd:schema targetNamespace="http://tempuri.org/Imports">
        <xsd:import schemaLocation="http://localhost/CustomSerialization/Service1.svc?xsd=xsd0" namespace="http://tempuri.org/" /> 
        <xsd:import schemaLocation="http://localhost/CustomSerialization/Service1.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/" /> 
        <xsd:import schemaLocation="http://localhost/CustomSerialization/Service1.svc?xsd=xsd2" namespace="http://schemas.biz.org/Samples/customserialization" /> 
        <xsd:import schemaLocation="http://localhost/CustomSerialization/Service1.svc?xsd=xsd3" namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays" /> 
      </xsd:schema>
    </wsdl:types>
    

    我不会在这里提供所有的xsd,但重点是有 它们中的AuxDomainData定义。 http://localhost/CustomSerialization/Service1.svc?xsd=xsd “文件”。 这就是问题所在。结果wsdl/xsd架构集不完整。

    那么,我的选择是什么?

    2 回复  |  直到 15 年前
        1
  •  0
  •   Aaronaught    15 年前

    您确定WSDL没有引用它吗?通常,WCF将在<wsdl:message>元素,有点像这样:

    <wsdl:types>
        <xsd:schema targetNamespace="http://tempuri.org/Imports">
            <xsd:import schemaLocation="http://localhost/CustomSerialization/Service1.svc?xsd=xsd0" namespace="http://tempuri.org/"/>
            <xsd:import schemaLocation="http://localhost/CustomSerialization/Service1.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
            <xsd:import schemaLocation="http://localhost/CustomSerialization/Service1.svc?xsd=xsd2" namespace="http://schemas.datacontract.org/2004/07/MyServiceNamespace"/>
            <xsd:import schemaLocation="http://localhost/CustomSerialization/Service1.svc?xsd=xsd3" namespace="http://schemas.datacontract.org/2004/07/MyServiceNamespace.AnotherNamespace"/>
            <xsd:import schemaLocation="http://localhost/CustomSerialization/Service1.svc?xsd=xsd4" namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/>
        </xsd:schema>
    </wsdl:types>
    

    检查一下这个。它应该在那里。但是,Java可能没有识别它。

    flatten the WSDL . 你可以试试。

    顺便说一下,mex端点通常是 Service1.svc/mex . WSDL略有不同。

        2
  •  0
  •   Shrike    15 年前

    http://blogs.msdn.com/dotnetinterop/archive/2008/09/23/flatten-your-wsdl-with-this-custom-servicehost-for-wcf.aspx “转向不完全正确。 不是想法本身,而是代码。在对多个服务使用该ExportExtension后,请求wsdl提供下一个服务,但由于类型重复而失败。

    我还在msdn论坛上问: http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/0ea27bec-08cc-4a20-86ce-6e3477abb1c5 .