0

我目前正在编写一个必须匹配现有 wsdl 的 C# web 服务,并且我的 web 服务由第三方 web 服务调用(我的 web 服务充当回调)。

但是我被传递了无效的值,第三方网络服务不愿意改变他们的网络服务。

我得到:'2010-10-24 12:12:13' 在 xml 中键入字符串作为 DateTime,(不符合规范,因为它必须是 '2010-10-24T12:12:13' ) 有没有办法覆盖 XML 序列化,使其仍然匹配 wsdl 但接受“任何东西”?

 [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public DateTime createdAt {
    get {
        return this.createdAt;
    }
    set {
        this.createdAtField = value;
    }
}
4

1 回答 1

0

您可以制作一个 SoapExtension 来修改soap xml,然后再被框架反序列化。如果您使用 wcf 和服务引用,则需要以不同的方式进行操作。这是我前段时间为清理消息而创建的一些代码。

在您的 app/web.config 中(这是用于清理来自肥皂服务的传入数据)

<system.web>
 <webServices>
  <soapExtensionTypes>
    <add type="mAdcOW.SoapCleanerModule.SOAPCleanerExtension, mAdcOW.SoapCleaner" />
  </soapExtensionTypes>
 </webServices>
</system.web>

以及在我的情况下删除非法 SOAP 字符的代码

using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Web.Services.Protocols;

namespace mAdcOW.SoapCleanerModule
{
    [AttributeUsage(AttributeTargets.Method)]
    public class SOAPCleaner : SoapExtensionAttribute
    {
        public override Type ExtensionType
        {
            get { return typeof (SOAPCleanerExtension); }
        }
        public override int Priority { get; set; }
    }

    public class SOAPCleanerExtension : SoapExtension
    {
        private static readonly Regex _reInvalidXmlChars = new Regex(@"&#x[01]?[0123456789ABCDEF];",
                                                                     RegexOptions.Compiled |
                                                                     RegexOptions.CultureInvariant);

        private Stream _originalStream;
        private MemoryStream _processStream;

        public override void Initialize(object initializer)
        {
        }

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

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

        public override Stream ChainStream(Stream stream)
        {
            _originalStream = stream;
            _processStream = new MemoryStream();
            return _processStream;
        }

        public override void ProcessMessage(SoapMessage message)
        {
            switch (message.Stage)
            {
                case SoapMessageStage.BeforeSerialize:
                    {
                        break;
                    }
                case SoapMessageStage.AfterSerialize:
                    {
                        // This is the message we send for our soap call
                        // Just pass our stream unmodified
                        _processStream.Position = 0;
                        Copy(_processStream, _originalStream);
                        break;
                    }
                case SoapMessageStage.BeforeDeserialize:
                    {
                        // This is the message we get back from the webservice
                        CopyAndClean(_originalStream, _processStream);
                        //Copy(_originalStream, _processStream);
                        _processStream.Position = 0;
                        break;
                    }
                case SoapMessageStage.AfterDeserialize:
                    break;
                default:
                    break;
            }
        }

        private void CopyAndClean(Stream from, Stream to)
        {
            TextReader reader = new StreamReader(from);
            TextWriter writer = new StreamWriter(to);
            string msg = reader.ReadToEnd();
            string cleanMsg = _reInvalidXmlChars.Replace(msg, "");
            writer.WriteLine(cleanMsg);
            writer.Flush();
        }

        private void Copy(Stream from, Stream to)
        {
            TextReader reader = new StreamReader(from);
            TextWriter writer = new StreamWriter(to);
            writer.WriteLine(reader.ReadToEnd());
            writer.Flush();
        }
    }
}
于 2010-11-02T21:17:37.000 回答