覆盖网络服务中传入的无效日期时间值

发布于 2024-10-01 00:54:38 字数 509 浏览 8 评论 0原文

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

但是,我传递的值无效,并且第三方网络服务不愿意更改其网络服务。

我得到: '2010-10-24 12:12:13' 在 xml 中键入字符串作为日期时间(这不符合规范,因为它必须是 '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;
    }
}

I'm currently writing a C# webservice that has to match an existing wsdl, and my webservice is called by a third party webservice (My webservice acts as a callback).

However I'm being passed invalid values, and the third party webservice is unwilling to change their webservice.

I'm getting: '2010-10-24 12:12:13' type strings in the xml as a DateTime, (which doesn't meet the spec as it has to be '2010-10-24T12:12:13')
Is there any way to override the XML Serialization so that it still matches the wsdl but accepts "anything"?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぺ禁宫浮华殁 2024-10-08 00:54:38

您可以创建一个 SoapExtension,在框架反序列化肥皂 xml 之前对其进行修改。如果您使用 wcf 和服务引用,则需要以不同的方式进行操作。这是我不久前为清理消息而创建的一些代码。

在你的 app/web.config 中(这是为了清理来自 SOAP 服务的传入数据)

<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();
        }
    }
}

You could make a SoapExtension which modifies the soap xml before it's being deserialized by the framework. If you use wcf and service reference you would need to do it differently. Here's some code I created to clean up a message some time ago.

In your app/web.config (this is for cleaning incoming data from a soap service)

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

and the code which in my case removes illegal SOAP characters

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();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文