【问题标题】:Prevent WCF from encoding strings in response?防止 WCF 编码字符串作为响应?
【发布时间】:2012-09-28 15:24:13
【问题描述】:

是否可以覆盖 WCF 中的设置以防止响应字符串中的 HTML 编码字符?

我有一个由第三方工具通过 SOAP 调用调用的简单服务。响应对象有一个由包含 XML 的字符串组成的属性。当 WCF 打包响应时,它变成:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<HttpPostResponse xmlns="http://ws.lenderprise.com">
  <HttpPostResult>
    &lt;STAT xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; &gt;
      &lt;RESPONSE Attr1=&quot;1&quot; 
             Attr2=&quot;ABC&quot; 
      &lt;/RESPONSE&gt;
    &lt;/STAT&gt;
  </HttpPostResult>
</HttpPostResponse>

是否可以不对包含的字符串进行编码?

<?xml version="1.0" encoding="ISO-8859-1" ?>
<HttpPostResponse xmlns="http://ws.lenderprise.com">
  <HttpPostResult>
    <STAT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
      <RESPONSE Attr1="1" 
             Attr2="ABC"> 
      </RESPONSE>
    </STAT>
  </HttpPostResult>
</HttpPostResponse>

我知道这仍然让我不得不确保处理任何嵌入的字符,并且这仍然意味着接收端的考虑。

【问题讨论】:

    标签: wcf html-encode


    【解决方案1】:

    如果要返回 XML 而不是字符串,则可以使用 XElementXmlElement 作为返回类型。在这种情况下,它不会被编码为字符串。

    【讨论】:

    • 我们可以阻止响应字符串参数的 HTMLEncoding 吗?
    • 可以,但不容易;您需要使用自定义消息格式化程序(在 CLR 对象(在本例中为字符串)和 WCF 消息之间进行转换的组件)。如果您想尝试这条路线,blogs.msdn.com/b/carlosfigueira/archive/2011/05/03/… 的帖子会提供更多信息。
    猜你喜欢
    • 1970-01-01
    • 2020-03-24
    • 2021-06-01
    • 2021-11-25
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    相关资源
    最近更新 更多