Invalid JSON being randomly returned by WCF
Asked Answered
P

2

6

I am running an ASP.Net app on Windows Server 2008 R2. I have up to .NET Framework 4.5 installed. Upon migrating from .NET Framework v2.0 to v4.0 I began to experience random behavior from WCF.

My web service returns JSON. Upon restarting IIS the service will return perfectly valid JSON about 3 to 5 times. After that, the JSON becomes invalid. I have inspected what is coming across the wire via Fiddler and also my own logs on both client app (an Adobe AIR app that calls the service) and on the server itself.

Fiddler shows that the raw response does in fact contain invalid JSON. It appears very much that a some random point in the JSON the response JSON begins to get written out again!, overwriting what should be there and producing mangled JSON in the process.

See this concatenated example:

{"responseCode":0,"actionCode":"OK","cdn{"responseCode":0, "actionCode".....

Note how "responseCode" (which should only appear at the beginning of the JSON response) suddenly appears again in the JSON further on. "cdn{"responseCode":0 is of course invalid JSON.

I have tried numerous patches. I've tried running ServiceModelReg.exe and aspnet_regiis.exe from under different frameworks and in different combinations. Same behavior. If I revert to previous code under v2.0 and switch the App Pool back to v2.0 then everything works fine.

I believe this to be a bug deep in WCF. Any ideas?

The only recourse I have at this point is to rip out WCF and switch to something like ServiceStack where I can debug into all the code if necessary.

Pornocracy answered 7/11, 2012 at 1:11 Comment(6)
Looks like some sort of buffering problem, where a buffer is retransmitted, or where a buffer isn't cleared after transmission/reception.Cotton
I am not aware of any existing issues on this. But it would be helpful to understand if you can post a piece of repro code.Teutonize
Have you tried FailedRequestLogging to see if there's anything fishy going through the process? Have you tried running your return objects through the JavaScriptSerializer() to see if there's a glitch in it? You can debug into the framework if you need to (see: msdn.microsoft.com/en-us/library/cc667410.aspx).Binoculars
Are you using some custom message inspectors (IDispatchMessageInspector) or anything like that that could mangle the response?Meath
did you also try SvcTraceViewer.exe msdn.microsoft.com/en-us/library/ms732023.aspx)?Kahler
I bet the part of the response that is invalid begins at the 128th character. Yes? We are seeing this same behavior with an ODATA service, it is outputting invalid xml. The first 128 characters are repeated in place of the 2nd 128 characters. Removing the diagnostics section in web.config as mentioned in one of the answers fixed it. There must be a bug in whatever that is doing that isn't handling buffers correctly.Arcade
D
1

Had the exact same problem. I fixed this by removing these 2 entries from my web.config:

<system.serviceModel>
    <diagnostics>
        <messageLogging logEntireMessage="true" logMessagesAtTransportLevel="true" maxSizeOfMessageToLog="-1" maxMessagesToLog="-1" />
    </diagnostics>
</system.serviceModel>

and

<system.diagnostics>
    <sources>
        <source name="System.ServiceModel.MessageLogging">
            <listeners>
                <add name="messages" type="MyCustomListener" />
            </listeners>
        </source>
    </sources>
</system.diagnostics>
Depredate answered 19/4, 2013 at 10:28 Comment(1)
Some of the solutions and comments above might have worked. I don't know. My final solution was to abandon WCF and use Service Stack. So far I am finding it is a superior architecture for implementing web services.Pornocracy
A
0

Are you setting InstanceContextMode to Single/PerSession?

Refer this... Mix up two responses

Antidromic answered 3/4, 2014 at 19:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.