I have two different types of WCF clients in my test environment sending a file to the WCF service using an operation like the following over basicHttpBinding:
void SendFile(string filename, byte[] fileBytes)
I noticed a HUGE difference in performance. For the exact same file and topology, SendFile takes less than 1s on Client1, but takes about 35-40s on Client2.
After some network sniffing, I narrowed the difference down to some line breaks in the base64 encoded content. Both clients send the fileBytes as Base64 encoded text. However Client2 somehow inserts many line-breaks in the text. I can consistently reproduce (using WFetch) that all else being the same, these line-breaks alone cause this huge performance difference.
Client1 message:
POST /ParkomatService/CommService HTTP/1.1
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://tempuri.org/ICommService/SendFile"
Host: 192.168.10.36
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS .NET CF Web Services Client Protocol 3.5.7283.0)
Cache-Control: No-Transform
Connection: Keep-Alive
Content-Length: 266863
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><SendFile xmlns="http://tempuri.org/"><filename>test.txt</filename><fileBytes>MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ...(continues)...EyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODk=</fileBytes></SendFile></s:Body></s:Envelope>
Client2 message:
POST /ParkomatService/CommService HTTP/1.1
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://tempuri.org/ICommService/SendFile"
Host: 192.168.10.36
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS .NET CF Web Services Client Protocol 3.5.7283.0)
Cache-Control: No-Transform
Connection: Keep-Alive
Content-Length: 273879
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><SendFile xmlns="http://tempuri.org/"><filename>test.txt</filename><fileBytes>MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2
Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIz
NDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkw
MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3
...
(con开发者_开发百科tinues)
...
OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1
Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODk=</fileBytes></SendFile></s:Body></s:Envelope>
Why would these line-breaks cause such a big difference in service processing times?
EDIT: With the help of Codo's comment below, I noticed this difference only occurs when debugging in Visual Studio. If I run the self-hosted service directly, then the line-breaks don't cause the performance hit. So, it must be an issue in Visual Studio.
It turns out the performance difference only happens when running the service with Visual Studio debugger attached. When ran outside VS, the difference went away and the version with line breaks had the same performance as otherwise.
I'm not sure why these lines breaks slow down VS so much but if you see any performance issues in your code, it is best to keep "running outside VS" on your list of things to try.
精彩评论