wcf forces chars escaping in response

2019-05-21 13:18发布

问题:


I want to return unescaped html in the WCF response, therefore I need CDATA section to be included in the response all the time.
I realized I have no chance with DataContractSerializer. So I have tried to mark my operation with [XmlSerializerFormat] and implement IXmlSerializable in the response class. I see that my serialization code is invoked but then it anyway does not work.
I am pretty sure now that WCF somehow analyzes the contents of the response and forces chars escaping there in a brutal way, ignoring my CDATA. Maybe I should go further and try some custom behavior implementation. Have u got any idea ? Tnx in advance.

回答1:

I don't exactly know if this helps, but I had a somewhat similar issue, where I needed to pass-through a JSON response from another service.

Since WCF apparently escapes quotes and other characters, which obviously was unwanted behavior in my case, I needed to simply ignore any help from WCF.

What I did was change my response type to System.ServiceModel.Channels.Message and then create a plain text message with System.ServiceModel.Web.WebOperationContext.Current.CreateTextResponse(responseText)

That did the trick! My message is now not interpreted by WCF, and my JSON response is unaltered.

More information here: http://msdn.microsoft.com/en-us/library/system.servicemodel.web.weboperationcontext.aspx