Enabling WCF logging causes responce corruption in webHttp service that returns a stream
-
Wednesday, September 12, 2012 2:28 PM
This only happens in WCF 4.5 RTM
If you create a servce that returns a stream as described in
How to: Create a Service That Returns Arbitrary Data Using The WCF Web HTTP Programming Model
[ServiceContract] public interface IImageService { [OperationContract, WebGet(UriTemplate = "/Test")] Stream GetImage(); } public class ImageService : IImageService { private const int BUFFER_SIZE = 1000; public Stream GetImage() { using (var file = File.OpenRead("Test.jpg")) { byte[] buffer = new byte[BUFFER_SIZE]; int offset = 0; int count; MemoryStream ms = new MemoryStream(); while ((count = file.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, count); offset += count; } ms.Flush(); ms.Position = 0L; WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg"; WebOperationContext.Current.OutgoingResponse.ContentLength = offset; return ms; } } } var host = new ServiceHost(typeof(ImageService), new Uri("http://localhost:8080/ImageService")); host.AddServiceEndpoint(typeof(IImageService), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior()); host.Open(); Console.WriteLine("Service is running"); Console.Write("Press ENTER to close the host"); Console.ReadLine(); host.Close();If you test it wilth Apache HTTP server benchmarking tool it will work fine under a light concurrent load:
ab.exe -n100 -c10 -X localhost:8888 http://localhost:8080/ImageService/Test
But if you enable WCF tracing as per Recommended Settings for Tracing and Message Logging and re-run the same test, some of the service responses will be corrupt. Service returns HTTP 200 and proper length on all of the responses, but it seems that the content byte array is shuffled or contaminated in some of them.
<configuration> <system.serviceModel> <diagnostics wmiProviderEnabled="true"> <messageLogging logEntireMessage="true" logMalformedMessages="true" logMessagesAtServiceLevel="false" logMessagesAtTransportLevel="true" maxMessagesToLog="5000" /> </diagnostics> </system.serviceModel> <system.diagnostics> <sources> <source name="System.ServiceModel" switchValue="Warning" propagateActivity="true" > <listeners> <add name="xml"/> </listeners> </source> <source name="System.ServiceModel.MessageLogging"> <listeners> <add name="xml" /> </listeners> </source> </sources> <sharedListeners> <add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="service.svclog" /> </sharedListeners> <trace autoflush="true" /> </system.diagnostics> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>Thanks for help.
All Replies
-
Wednesday, September 12, 2012 3:39 PM
-
Wednesday, September 12, 2012 3:40 PM
-
Friday, September 14, 2012 8:27 AMModerator
Hi govis,
Thanks for posting. Unfortunately, I can't reproduce this issue.
If your problem persists, I'd suggest you open a support case via http://support.microsoft.com/ph/548 or submit a feedback at connect site, a dedicated engineer will help you to resolve this issue. Thanks.
Leo Tang [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help. -
Friday, September 14, 2012 1:45 PM
Hi Leo,
Thanks for looking into this. It seems to be happening to some streams but not the others. It happens to text streams too (ContentType = "application/xml"). It might have something to do with the size or some bytes in the stream itself. Can you please try the image I attached (Right-click on the blue circle, and select Save Picture As...).
Thanks.
-
Tuesday, September 18, 2012 7:31 PMOwner
Thanks govis for reporting this! WCF product team has identified this as an issue with ByteStreamMessage encoder which is used with WebHttpBinding in 4.5. We are working to identify a fix for this right now. We couldn't think of any safe workaround to unblock you right now.
- Piyush
- Marked As Answer by LeoTangModerator Thursday, September 20, 2012 9:31 AM
-
Friday, October 26, 2012 9:06 AMWe got hit by this also. Is there any updated on this how/when will this be fixed? Any official bug report I can track?
-
Wednesday, November 28, 2012 1:25 PM
Does someone submit the issue?
We've faced with the same issue.

