1

关于这个问题有很多讨论,但是我现在已经尝试了所有可能的解决方案,我们仍然从服务器收到 413 Request Entity Too Large 错误。

我们的 WCF 服务通过 Azure Worker 角色自托管,不使用 IIS。我们所有的配置都在代码中指定:

var host = new ServiceHost(searchEngine);

// Create binding
var binding = new WebHttpBinding(WebHttpSecurityMode.Transport);

binding.MaxReceivedMessageSize = 2147483647;
binding.MaxBufferSize = 2147483647;
binding.MaxBufferPoolSize = 2147483647;

var readerQuotas = new XmlDictionaryReaderQuotas
{
    MaxStringContentLength = 2147483647,
    MaxArrayLength = 2147483647,
    MaxBytesPerRead = 2147483647,
    MaxDepth = 2147483647,
    MaxNameTableCharCount = 2147483647
};

// Setting quotas on a BindingElement after the binding is created has no effect on that binding.
// See: https://stackoverflow.com/questions/969479/modify-endpoint-readerquotas-programatically
binding.GetType().GetProperty("ReaderQuotas").SetValue(binding, readerQuotas, null);

binding.ReceiveTimeout = new TimeSpan(1, 0, 0);
binding.SendTimeout = new TimeSpan(1, 0, 0);

// Add the service endpoint
var ep = host.AddServiceEndpoint(
    typeof(ISearchEngine),
    binding,
    string.Format("https://{0}/SearchEngine", externalEndpoint));

ep.Behaviors.Add(new WebHttpBehavior());

// Increase the MaxItemsInObjectGraph quota for all operations in this service
foreach (var operation in ep.Contract.Operations)
{
    operation.Behaviors.Find<DataContractSerializerOperationBehavior>().MaxItemsInObjectGraph = 10000000;
}

return host;

这是我们的客户端配置 - 也在代码中指定:

var binding = new WebHttpBinding(WebHttpSecurityMode.Transport);

binding.MaxReceivedMessageSize = 2147483647;
binding.MaxBufferSize = 2147483647;
binding.MaxBufferPoolSize = 2147483647;

var readerQuotas = new XmlDictionaryReaderQuotas
{
    MaxStringContentLength = 2147483647,
    MaxArrayLength = 2147483647,
    MaxBytesPerRead = 2147483647,
    MaxDepth = 2147483647,
    MaxNameTableCharCount = 2147483647
};

// Setting quotas on a BindingElement after the binding is created has no effect on that binding.
// See: https://stackoverflow.com/questions/969479/modify-endpoint-readerquotas-programatically
binding.GetType().GetProperty("ReaderQuotas").SetValue(binding, readerQuotas, null);

binding.ReceiveTimeout = new TimeSpan(1, 0, 0);
binding.SendTimeout = new TimeSpan(1, 0, 0);

binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

var channelFactory = new ChannelFactory<ISearchEngine>(binding, endpointAddress);

channelFactory.Endpoint.Behaviors.Add(new WebHttpBehavior());


// Increase the MaxItemsInObjectGraph quota for all operations in this service
foreach (var operation in channelFactory.Endpoint.Contract.Operations)
{
    operation.Behaviors.Find<DataContractSerializerOperationBehavior>().MaxItemsInObjectGraph = 10000000;
}

return channelFactory.CreateChannel();

我唯一的预感可能是 SSL 连接有问题?有一些文章提到了特定于 IIS 的问题,但是我不确定这是否与自托管服务有关。

非常感谢任何建议。

更新:

为了确认 SSL 是问题的预感,我暂时禁用了 SSL,然后发现问题消失了。

所以现在我需要弄清楚为什么 SSL 会导致问题。有很多关于类似问题的文档,但它仅与 IIS 托管服务有关(我们的服务是从 Windows 服务自托管的):

IIS7 - (413) 请求实体太大 | 上传ReadAheadSize

那里有人知道仅适用于自托管 WCF 服务的等效设置吗?

4

3 回答 3

3

由于这个看似无关的帖子,我发现了问题:

http://forums.newatlanta.com/messages.cfm?threadid=554611A2-E03F-43DB-92F996F4B6222BC0&#top

这绝对是一个 SSL 问题,它与将 SSL 证书绑定到您托管的端口有关。您必须使用 netsh 绑定证书并将clientcertnegotiation=enable添加到绑定中。

在我们的例子中,我们已经在使用 netsh,因为我们使用了不同的端口,所以我们的绑定现在看起来像这样:

netsh http add sslcert ipport=0.0.0.0:10100 certhash=000000089A6679262D845B650FDDE5390F0D86AB appid={000007b4-2d4b-4587-ae99-7a6627476f76} clientcertnegotiation=enable

对于那些通过 IIS 托管并更改 UploadReadAheadSize 值的人,上面的论坛帖子指出这可能会导致 CPU 过高,而这个解决方案可能会更好。

于 2014-03-06T06:05:55.537 回答
1

在需要传输大数据的情况下,应使用 transferMode = "Streaming"。

看看 MS 的这篇论文:

http://msdn.microsoft.com/en-us/library/ms733742(v=vs.110).aspx

于 2014-03-05T16:54:03.960 回答
0
<webHttpBinding>
   <binding name="TransportSecurity" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
        <security mode="Transport" />
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="16384" />
   </binding>
于 2022-02-08T11:23:22.313 回答