WCF MTOM消除了领先的空白

时间:2020-11-09 09:02:54

标签: c# wcf mtom

根据这篇文章...

Space disappears on transferring via wcf (xml)

...当在WCF中使用MTOM绑定发送带有前导空格(例如 foo)的字符串时,存在一个配置错误。

我尝试使用给定的建议修复该错误,但还没有运气。这是我的代码:

我们使用的绑定:

<binding name="foo_wsHTTPbinding_MitSSL_MTOM" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" sendTimeout="00:02:00" receiveTimeout="00:01:00" openTimeout="00:01:00" closeTimeout="00:01:00" messageEncoding="Mtom">
    <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647" />
    <security mode="Transport">
        <transport clientCredentialType="Windows" proxyCredentialType="Windows" />
        <message clientCredentialType="Windows" />
    </security>
</binding>

我们添加的代码:

在配置内部:

<extensions>
    <behaviorExtensions>
        <add name="preserveLeadingSpacesBehavior" type="Core.ServiceBase.PreserveLeadingSpacesBehaviorExtensionElement, Core.ServiceBase"/>
    </behaviorExtensions>
</extensions>
<behaviors>
    <serviceBehaviors>[...]</serviceBehaviors>
    <endpointBehaviors>
        [...]
        <behavior>
            <preserveLeadingSpacesBehavior />
        </behavior>
    </endpointBehaviors>
</behaviors>

PreserveLeadingSpacesBehaviorExtensionElement.cs:

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

namespace Core.ServiceBase
{
    public class PreserveLeadingSpacesBehaviorExtensionElement : BehaviorExtensionElement
    {
        protected override object CreateBehavior()
        {
            return new PreserveLeadingSpacesBehavior();
        }

        public override Type BehaviorType
        {
            get { return typeof(PreserveLeadingSpacesBehavior); }
        }
    }

    public class PreserveLeadingSpacesBehavior : IEndpointBehavior
    {
        public void Validate(ServiceEndpoint endpoint)
        {
        }

        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new PreserveLeadingSpacesInspector());
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
        }
    }

    public class PreserveLeadingSpacesInspector : IDispatchMessageInspector
    {
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            request = request.CreateBufferedCopy(int.MaxValue).CreateMessage();
            return null;
        }

        public void BeforeSendReply(ref Message reply, object correlationState)
        {
        }
    }
}

根据博客文章here,这应该可以解决问题。

但是到目前为止,它还没有起作用。设置断点时,它在AfterReceiveRequest中被命中,并且在消息中我看到空格仍然存在,但是在我的服务方法中,它消失了。

在服务器站点上使用我的测试方法:

public string Foo(string bar)
{
    return bar;
}

您有什么想法,为什么它仍然无法工作?

0 个答案:

没有答案