。NET Framework-WCF-MTOM-防止多部分/相关

问题描述 投票:0回答:2

全部,

我想使用需要MTOM来将文件作为附件传输的SOAP XML服务。我仅控制启动器,而不控制服务器端。使用服务时,当服务器端不支持此消息(并且无法在我的控件中对其进行修改)时,.NET Framework会将消息自动转换为多部分消息。应用此行为时,我找不到阈值,也无法找到允许我防止这种情况的设置。附加文档的大小是有限的,因此简单格式application / xop + xml不会导致任何问题或性能损失。

一些代码段:

var endpointAddress = new EndpointAddress(GetUri());
var binding = new WSHttpBinding();
binding.MessageEncoding = WSMessageEncoding.Mtom;
var stackOverflowChannel = ChannelFactory<MyInterface>.CreateChannel(binding, endpointAddress);

stackOverflowChannel.ConsumeMe(request);

非常感谢所有帮助。

c# wcf soap .net-framework-version mtom
2个回答
0
投票

[通常,如果服务器启用MTOM编码器,则您上面编写的客户端代码将正常工作。要注意的另一件事是Wshttpbinding默认使用消息层安全性,并且要求客户端提供Windows凭据。

//logins account on the server side.
            client.ClientCredentials.Windows.ClientCredential.UserName = "administrator";
            client.ClientCredentials.Windows.ClientCredential.Password = "abcd1234!";

最后,我建议您使用客户端代理来调用远程服务。https://docs.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client客户端通过adding the service reference工具在Reference.cs中生成代理类和用于服务的配置文件。配置文件位于appconfig/webconfig中,并且其中包含的绑定配置与服务器一致。我们可以使用此方法确定服务器的配置(是否支持Mtom)。在我的客户端上,它按预期工作。

ServiceReference1.ServiceClient client = new ServiceClient();
            //logins account on the server side.
            client.ClientCredentials.Windows.ClientCredential.UserName = "administrator";
            client.ClientCredentials.Windows.ClientCredential.Password = "abcd1234!";
            Console.WriteLine(client.Test());

Fiddler捕获的SOAP消息。enter image description here我想知道调用远程服务的错误详细信息,请随时让我知道问题是否仍然存在。


0
投票

感谢您的回复。昨天晚上我发现了问题...显然,使用MTOM传输消息时,它总是作为多部分消息发送。 Microsoft DLL中有一个硬编码的边界,它会切换为从767字节开始的多部分。这在XmlMtomWriter.cs中。代码段:

    class XmlMtomWriter : XmlDictionaryWriter, IXmlMtomWriterInitializer
    {
        // Maximum number of bytes that are inlined as base64 data without being MTOM-optimized as xop:Include
        const int MaxInlinedBytes = 767;  // 768 will be the first MIMEd length

        int maxSizeInBytes;
        XmlDictionaryWriter writer;
        XmlDictionaryWriter infosetWriter;
        MimeWriter mimeWriter;

此值是硬编码的,不能覆盖,除非您将创建自己的序列化工厂。

在我的情况下,这个问题是没有问题的。服务器端的验证代码中有一个错误,该错误假定发送附件时必须为“单一格式”。

通过重新配置他们的终端,我得以解决他们的错误,现在一切都按预期运行。

服务器端是XDSToolkit(XDSTools)。

这不是安全问题,因为通信确实成功。

谢谢您的回答和时间。

© www.soinside.com 2019 - 2024. All rights reserved.