Saturday, December 29, 2012

WCF streaming: Random MemoryStream corruption

This one bit me a while ago and I responded to a Stack Overflow post about it - the original poster had changed their implementation to avoid the issue, and slightly missed the point of my answer - which was a solution to a very real (and annoying problem).

As the post describes, returning a MemoryStream from a WCF service can result in the caller receiving a corrupted response. But, as I discovered, only when WCF tracing is active in web.config. So, the solution is simple enough - make sure that web.config does not include a system.diagnostics element. Further testing revealed that this (Microsoft) defect is still present in .NET 4.5.

 Stack overflow post: http://stackoverflow.com/questions/13278487/memorystream-data-corruption-issue

2 comments:

Nick Quinn said...

Hey Tony, I came across the question on SO before I found your site but unfortunately the original poster did not take your advice and did not mark it as the correct answer so I missed your answer on there, but found your blog post later on. You are spot on and thank you for saving me from near nervous breakdown; I spent the best part of three hours trying to figure out why my client received response was bad when the server's sent response was good.

Tony Beveridge said...

Hi Nick:
Glad it helped - I fully understand the frustration you felt, it really irked me as well!
Cheers
Tony