5

帮助!我有一个 C# 应用程序正在使用的 Axis Web 服务。一切都很好,除了长值数组总是出现 [0,0,0,0] - 正确的长度,但值没有反序列化。我尝试过使用其他原语(整数、双精度数)并且发生了同样的事情。我该怎么办?我不想改变我的服务的语义。

4

3 回答 3

7

这就是我最终的结果。我从来没有为此找到另一个解决方案,所以如果你有更好的东西,一定要贡献。

首先,wsdl:types 区域中的长数组定义:

  <xsd:complexType name="ArrayOf_xsd_long">
    <xsd:complexContent mixed="false">
      <xsd:restriction base="soapenc:Array">
        <xsd:attribute wsdl:arrayType="soapenc:long[]" ref="soapenc:arrayType" />
      </xsd:restriction>
    </xsd:complexContent>
  </xsd:complexType>

接下来,我们创建一个将执行修复的 SoapExtensionAttribute。似乎问题在于 .NET 没有将 multiref id 跟踪到包含 double 值的元素。因此,我们处理数组项,查找值,然后将值插入到元素中:

[AttributeUsage(AttributeTargets.Method)]
public class LongArrayHelperAttribute : SoapExtensionAttribute
{
    private int priority = 0;

    public override Type ExtensionType
    {
        get { return typeof (LongArrayHelper); }
    }

    public override int Priority
    {
        get { return priority; }
        set { priority = value; }
    }
}

public class LongArrayHelper : SoapExtension
{
    private static ILog log = LogManager.GetLogger(typeof (LongArrayHelper));

    public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
    {
        return null;
    }

    public override object GetInitializer(Type serviceType)
    {
        return null;
    }

    public override void Initialize(object initializer)
    {
    }

    private Stream originalStream;

    private Stream newStream;

    public override void ProcessMessage(SoapMessage m)
    {
        switch (m.Stage)
        {
            case SoapMessageStage.AfterSerialize:
                newStream.Position = 0; //need to reset stream 
                CopyStream(newStream, originalStream);
                break;

            case SoapMessageStage.BeforeDeserialize:
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = false;
                settings.NewLineOnAttributes = false;
                settings.NewLineHandling = NewLineHandling.None;
                settings.NewLineChars = "";
                XmlWriter writer = XmlWriter.Create(newStream, settings);

                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(originalStream);

                List<XmlElement> longArrayItems = new List<XmlElement>();
                Dictionary<string, XmlElement> multiRefs = new Dictionary<string, XmlElement>();
                FindImportantNodes(xmlDocument.DocumentElement, longArrayItems, multiRefs);
                FixLongArrays(longArrayItems, multiRefs);

                xmlDocument.Save(writer);
                newStream.Position = 0;
                break;
        }
    }

    private static void FindImportantNodes(XmlElement element, List<XmlElement> longArrayItems,
                                           Dictionary<string, XmlElement> multiRefs)
    {
        string val = element.GetAttribute("soapenc:arrayType");
        if (val != null && val.Contains(":long["))
        {
            longArrayItems.Add(element);
        }
        if (element.Name == "multiRef")
        {
            multiRefs[element.GetAttribute("id")] = element;
        }
        foreach (XmlNode node in element.ChildNodes)
        {
            XmlElement child = node as XmlElement;
            if (child != null)
            {
                FindImportantNodes(child, longArrayItems, multiRefs);
            }
        }
    }

    private static void FixLongArrays(List<XmlElement> longArrayItems, Dictionary<string, XmlElement> multiRefs)
    {
        foreach (XmlElement element in longArrayItems)
        {
            foreach (XmlNode node in element.ChildNodes)
            {
                XmlElement child = node as XmlElement;
                if (child != null)
                {
                    string href = child.GetAttribute("href");
                    if (href == null || href.Length == 0)
                    {
                        continue;
                    }
                    if (href.StartsWith("#"))
                    {
                        href = href.Remove(0, 1);
                    }
                    XmlElement multiRef = multiRefs[href];
                    if (multiRef == null)
                    {
                        continue;
                    }
                    child.RemoveAttribute("href");
                    child.InnerXml = multiRef.InnerXml;
                    if (log.IsDebugEnabled)
                    {
                        log.Debug("Replaced multiRef id '" + href + "' with value: " + multiRef.InnerXml);
                    }
                }
            }
        }
    }

    public override Stream ChainStream(Stream s)
    {
        originalStream = s;
        newStream = new MemoryStream();
        return newStream;
    }

    private static void CopyStream(Stream from, Stream to)
    {
        TextReader reader = new StreamReader(from);
        TextWriter writer = new StreamWriter(to);
        writer.WriteLine(reader.ReadToEnd());
        writer.Flush();
    }
}

最后,我们标记 Reference.cs 文件中的所有方法,这些方法将使用我们的属性反序列化一个长数组:

    [SoapRpcMethod("", RequestNamespace="http://some.service.provider",
        ResponseNamespace="http://some.service.provider")]
    [return : SoapElement("getFooReturn")]
    [LongArrayHelper]
    public Foo getFoo()
    {
        object[] results = Invoke("getFoo", new object[0]);
        return ((Foo) (results[0]));
    }

这个修复是长期特定的,但它可能被推广到处理任何有这个问题的原始类型。

于 2008-08-27T13:54:06.453 回答
3

这是我写的关于该主题的博客文章的或多或少的复制粘贴版本。

执行摘要:您可以更改 .NET 反序列化结果集的方式(请参阅上面 Chris 的解决方案),或者您可以重新配置 Axis 以以与 .NET SOAP 实现兼容的方式序列化其结果。

如果你走后一条路线,方法如下:

...生成的类看起来运行正常,但是如果您查看客户端(.NET/WCF)端的反序列化数组,您会发现该数组已被错误地反序列化,并且所有值都在数组为 0。您必须手动查看 Axis 返回的 SOAP 响应以找出问题所在;这是一个示例响应(再次为清楚起见进行了编辑):

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/>
    <soapenv:Body>
        <doSomethingResponse>
          <doSomethingReturn>
            <doSomethingReturn href="#id0"/>
            <doSomethingReturn href="#id1"/>
            <doSomethingReturn href="#id2"/>
            <doSomethingReturn href="#id3"/>
            <doSomethingReturn href="#id4"/>
          </doSomethingReturn>
        </doSomethingResponse>
        <multiRef id="id4">5</multiRef>
        <multiRef id="id3">4</multiRef>
        <multiRef id="id2">3</multiRef>
        <multiRef id="id1">2</multiRef>
        <multiRef id="id0">1</multiRef>
   </soapenv:Body>
</soapenv:Envelope>

您会注意到 Axis 不会直接在返回的元素中生成值,而是引用外部元素获取值。当有很多引用相对较少的离散值时,这可能是有道理的,但无论如何,WCF basicHttpBinding 提供程序都没有正确处理这件事(据报道,gSOAP 和经典的 .NET Web 引用也是如此)。

我花了一段时间才找到解决方案:编辑 Axis 部署的 server-config.wsdd 文件并找到以下参数:

<parameter name="sendMultiRefs" value="true"/>

将其更改为 false,然后通过命令行重新部署,它(在 Windows 下)看起来像这样:

java -cp %AXISCLASSPATH% org.apache.axis.client.AdminClient server-config.wsdl 

Web 服务的响应现在应该可以由您的 .NET 客户端反序列化。

于 2009-03-16T12:39:56.877 回答
1

发现此链接可能提供更好的选择: http ://www.tomergabel.com/GettingWCFAndApacheAxisToBeFriendly.aspx

于 2009-02-20T17:17:09.953 回答