2012-03-19 91 views
2

當比較使用REST服務和SOAP服務的客戶端時,我得到的性能結果無法解釋。我所做的是創建一個服務代理如下:REST服務代理與WCF SOAP代理的性能

REST:

WebHttpBinding webBinding = new WebHttpBinding(); 
webBinding.AllowCookies = true; 
webBinding.MaxReceivedMessageSize = int.MaxValue; 

CustomBinding custom = new CustomBinding(webBinding); 

WebMessageEncodingBindingElement webMEBE = custom.Elements.Find<WebMessageEncodingBindingElement>(); 

webMEBE.ContentTypeMapper = new MyMapper(); 
webMEBE.ReaderQuotas.MaxArrayLength = int.MaxValue; 

var factory = new WebChannelFactory<ITest>(custom, new Uri("http://localhost/Test")); 
var proxy = factory.CreateChannel(); 

SOAP:

endPointAddr = "net.tcp://" + textBox2.Text + 
":8909/MyService"; 
tcpBinding = new NetTcpBinding(); 
tcpBinding.MaxReceivedMessageSize = int.MaxValue; 
tcpBinding.ReaderQuotas.MaxArrayLength = int.MaxValue; 
tcpBinding.TransactionFlow = false; 
tcpBinding.Security.Transport.ProtectionLevel = 
    System.Net.Security.ProtectionLevel.EncryptAndSign; 
tcpBinding.Security.Transport.ClientCredentialType = 
    TcpClientCredentialType.Windows; 
tcpBinding.Security.Mode = SecurityMode.None; 

endpointAddress = 
    new EndpointAddress(endPointAddr); 

IService1 proxy = 
    ChannelFactory<IService1>.CreateChannel(tcpBinding, endpointAddress); 

兩個IService1ITest有一個方法,我使用,GetRequest(),它返回一個〜 300Kb的對象。 IService1.GetRequest()是一個OperationContract,ITest.GetRequest()是一個WebGet。

一旦我在這兩種情況下打開通道,我運行了一個緊密的proxy.GetRequest()循環,以確定每個請求可以處理多少個請求。結果是,如果測試在本地機器上以5:1的速度超過REST,並且通過網絡,SOAP仍然比REST高出約50%。

我不明白爲什麼會有這麼大的差異。

+1

您的SOAP不是SOAP。 – 2012-03-19 16:49:59

+0

尷尬:S。星期五,這是我第一次使用這兩種技術之一,並且從我在搜索WCF SOAP時發現的一個教程中獲益。 – user472875 2012-03-19 16:57:10

回答

4

區別是因爲您使用的綁定。您正在使用net.tcp作爲您的SOAP服務的協議,它使用專有的二進制格式傳輸消息。這意味着非WCF技術將無法連接到您的服務。 REST服務正在使用WebHttp,該服務將與其他(非.NET)技術更兼容,但不會具有相同的性能。

如果您想要更好地比較SOAP與REST性能的「蘋果對蘋果」比較,請使用WsHttpBindingBasicHttpBinding作爲您的SOAP服務。

+0

這解釋了很多。問題是我正在做這些測試,因爲我們想切換到REST,因爲它不需要WCF。我只是不確定我們能否承受這種表現。有沒有任何平臺不可知論的技術,會給我速度類似於我得到的WCF? – user472875 2012-03-19 16:45:58

+0

比我的答案更好 - 我喜歡蘋果對蘋果的觀點。 – 2012-03-19 16:54:14

+0

@user不,這是性能和互操作性之間的折衷。你也將失去所有'WS- *'功能,比如定義事務範圍,更多的面向安全的功能等等。 – 2012-03-19 17:17:53