2014-02-18 40 views
0

我想能夠發送大量的數據到我的WCF service,我發送的是List<byte[]>,客戶端收到並且當前我的應用程序得到一個錯誤,套接字中止。WCF和大量的數據

,所以我發現部份螺紋:http://geekswithblogs.net/tmurphy/archive/2011/08/15/sending-large-arrays-with-wcf.aspx

這是我的連接是如何estanlished:

string urlService = "net.tcp://localhost:8000/myApp"; 
ServiceHost host = new ServiceHost(typeof(pp.classes.service1)); 
host.Opening += new EventHandler(host_Opening); 
host.Opened += new EventHandler(host_Opened); 
host.Closing += new EventHandler(host_Closing); 
host.Closed += new EventHandler(host_Closed); 

NetTcpBinding tcpBinding = new NetTcpBinding(); 
tcpBinding.TransactionFlow = false; 
tcpBinding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign; 
tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows; 
tcpBinding.Security.Mode = SecurityMode.None; // <- Very crucial 
pp.classes.service1 ser = new pp.classes.service1(); 
host.AddServiceEndpoint(typeof(pp.classes.IService1), tcpBinding, urlService); 
ServiceMetadataBehavior metadataBehavior; 
metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>(); 
if (metadataBehavior == null) 
{ 
    // Create the proxy object that is generated via the svcutil.exe tool 
    metadataBehavior = new ServiceMetadataBehavior(); 
    metadataBehavior.HttpGetUrl = new Uri("http://localhost:8001/myApp"); 
    metadataBehavior.HttpGetEnabled = true; 
    metadataBehavior.ToString(); 
    host.Description.Behaviors.Add(metadataBehavior); 
    string urlMeta = metadataBehavior.HttpGetUrl.ToString(); 
} 

host.Open(); 

我試圖找到maxReceivedMessageSizemaxStringContentLengthmaxArrayLength性質,但我發現只有maxReceivedMessageSize(我工作不app.config文件)

+0

我沒有一個,我沖水WCF服務在我的WinForms GUI – user3271698

+0

可能重複的:http://stackoverflow.com/questions/969479/modify-endpoint-readerquotas-programatically所有我的 – ilansch

+0

第一愛評論* < - 非常關鍵*沒有任何進一步解釋爲什麼。那很棒。你也應該將WCF配置添加到你的'app.config'中,這使配置變得更容易(是的,即使你自己託管服務,你也可以這麼做)。第三,你可能會想要使用流式服務,它允許你通過流傳輸大量的數據。這個問題似乎總結得很好:http://stackoverflow.com/questions/14479885/wcf-streaming-large-data-500mb-1gb-on-a-self-hosted-service –

回答

0

在代碼中使用此配置。它將讀取器配額設置爲最大值:

XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas(); 
quotas.MaxDepth = 2147483647; 
quotas.MaxStringContentLength = 2147483647; 
quotas.MaxArrayLength = 2147483647; 
quotas.MaxBytesPerRead = 2147483647; 
quotas.MaxNameTableCharCount = 2147483647; 
tcpBinding.ReaderQuotas = quotas; 

使用此設置超時。它會將超時設置爲5分鐘。

tcpBinding.ReceiveTimeout = new TimeSpan(0, 5, 0); 
tcpBinding.SendTimeout = new TimeSpan(0, 5, 0); 
+0

只需複製此代碼並將其放在tcpBinding旁邊? – user3271698

+0

您應該將此與您當前的綁定進行整合。設置綁定的「配額」。 –

+0

你能告訴我怎麼做? (我是一個新開發者...) – user3271698