0

我整天都在寻找解决问题的方法(也在 StackOverflow 上),但不幸的是没有任何效果。我仍然收到错误:

“已超出传入消息 (65536) 的最大消息大小配额。要增加配额,请在适当的绑定元素上使用 MaxReceivedMessageSize 属性。”

有数百种解决方案,但基于项目中的 .config 文件。我有 WCF 服务和 Silverlight 客户端。它们之间的绑定仅以编程方式设置。

这是 WCF 服务配置的块代码:

private ServiceHostBase CreateService(Uri baseAddress)
    {
        var serviceHost = new ServiceHost(typeof(MyService), new[] { baseAddress });
        var endPointWithoutSSL = new BasicHttpBinding()
        {
            MaxBufferSize = int.MaxValue,
            MaxReceivedMessageSize = int.MaxValue,
            MaxBufferPoolSize = int.MaxValue,
        };

        serviceHost.AddServiceEndpoint(typeof(MyService), endPointWithoutSSL,                                       baseAddress.ToString());
        return serviceHost;
    }

在 Silverlight 项目中,客户端端点的配置如下所示:

private BasicHttpBinding GetBinding()
    {
        var securityMode = GetSecurityMode();
        var binding = new BasicHttpBinding(securityMode)
        {
            SendTimeout = TimeSpan.FromMinutes(10),
            OpenTimeout = TimeSpan.FromMinutes(10),
            CloseTimeout = TimeSpan.FromMinutes(10),
            ReceiveTimeout = TimeSpan.FromMinutes(10),
            TextEncoding = Encoding.UTF8,
            TransferMode = TransferMode.Buffered,
            MaxReceivedMessageSize = int.MaxValue,
            MaxBufferSize = int.MaxValue,
        };

        return binding;
    }

无论我多么努力,MaxReceivedMessageSize 从客户端设置为 65k。Microsoft WCF 跟踪工具显示,在引发超出最大接收消息大小的异常后。

更有趣的是,在端点中启用 BasicHttpSecurityMode.Transport 不会导致此错误。但是,我必须设置没有 BasicHttpSecurityMode.Transport 选项的端点。

任何帮助,将不胜感激。

谢谢

4

1 回答 1

1

在 WCF 端尝试下面的代码,它应该可以工作。

    private ServiceHostBase CreateService(Uri baseAddress)
    {
        var serviceHost = new ServiceHost(typeof(MyService), new[] { baseAddress });
        var endPointWithoutSSL = new BasicHttpBinding()
        {
            MaxBufferSize = int.MaxValue,
            MaxReceivedMessageSize = int.MaxValue,
            MaxBufferPoolSize = int.MaxValue,
        };

        endPointWithoutSSL.ReaderQuotas.MaxStringContentLength = int.MaxValue;
        endPointWithoutSSL.ReaderQuotas.MaxNameTableCharCount = int.MaxValue;
        endPointWithoutSSL.ReaderQuotas.MaxDepth = int.MaxValue;
        endPointWithoutSSL.ReaderQuotas.MaxBytesPerRead = int.MaxValue;
        endPointWithoutSSL.ReaderQuotas.MaxArrayLength = int.MaxValue;

        serviceHost.AddServiceEndpoint(typeof(MyService), endPointWithoutSSL, baseAddress.ToString());
        return serviceHost;
    }
于 2014-01-23T07:49:05.093 回答