2014-03-05 118 views
1

關於此問題有許多討論,但是我現在嘗試了所有可能的解決方案,並且我們仍然從服務器獲得413請求實體太大的錯誤。WCF 413請求實體太大 - 自託管WebHttpBinding

我們的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的問題,但我不確定這是否與自託管服務有關。

非常感謝您的任何建議。

UPDATE:

爲了證實我的預感,SSL是問題,我暫時禁用SSL你瞧問題就消失了。

所以現在我需要弄清楚爲什麼SSL會導致這個問題。有有關類似問題的文檔還是比較可觀的,但它涉及到IIS只託管服務(我們的自我從Windows服務託管):

IIS7 - (413) Request Entity Too Large | uploadReadAheadSize

會有人在那裏認識的等效設置,將僅適用於自託管的WCF服務?

回答

3

我發現這個問題,這要歸功於這個看似不相關的職位:

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

這絕對是一個SSL的問題,它是將SSL證書綁定到您託管的端口上。您必須使用netsh綁定證書並添加clientcertnegotiation =啓用綁定。

在我們的例子中,我們已經使用netsh的,因爲我們用的是不同的端口,所以我們現在結合看起來是這樣的:

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

對於那些通過IIS託管,並改變應將UploadReadAheadSize的價值,上面的論壇帖子指出,這可能會導致高CPU,相反,這種解決方案可能會更好。

1

如果您需要傳輸大量數據,則應使用transferMode =「Streaming」。

看看本文從MS:

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

+0

謝謝。我也試過這個,但後來發現Streaming模式不適用於我們正在使用的基本認證。也就是說,我們的數據很小:100kb - 200kb,所以我認爲這個問題並不缺乏Streaming。 – antscode