【问题标题】:Returning Plain old XML with WCF使用 WCF 返回普通的旧 XML
【发布时间】:2011-10-22 09:52:03
【问题描述】:

这怎么会这么难,为什么?返回没有名称空间和 XML 声明的简单 XML。使用 XML Writer 创建 XML 很容易,如果我将其输出到文件中,那就太好了。到底如何通过 WCF 返回内容。使用 XML 元素不好,因为您松开了 XML 声明,而使用字符串也不好,因为输出包装在 元素中。无法返回 XML 文档,因为它无法被序列化。

我知道这个网站上有很多帖子,但没有一个回答这个问题。我正在使用 VB.NET(男孩,我希望我有时间学习 C#),即使使用 IXmlSerializer,我也无法让 Data Contract 工作。我需要通过 WCF 服务发回的输出示例如下:

<?xml version="1.0" encoding="utf-8"?>
 <BookingResponse timestamp="" success="1">
<confirmation id="" track_code="" status="" notes="" tracking_url="" confirmed_at=""/>
 </BookingResponse>

不幸的是,我发送到的服务将不接受除此纯 XML 之外的任何内容。将不接受 WCF 似乎自行放入的其他名称空间。

我正在使用适用于 Visual Studio 2010 的 VB 在线休息模板。

帮助!请!一定有人有答案吗?

这里有一些代码:

<WebInvoke(UriTemplate:="BookJob", Method:="POST")>
        Public Function Create(ByVal XMLBooking As Stream) As Stream
            'DO SOME PROCESSING ON THE REQUEST - THIS ALL WORKS FINE.....

            Dim str As String

'This CreateResponseXML function creates a String version of the XML, which is built using XMLWriter.


       str = CreateResponseXML(True, vConfirmationID, sqlFuncs.BookingStatus("Confirmed"), "", "")

            Dim memoryStream As New MemoryStream()
            Using streamWriter As New StreamWriter(memoryStream)
                streamWriter.Write(str)
                streamWriter.Flush()

                memoryStream.Position = 0

                Return memoryStream
            End Using

        End Function

为了增加麻烦,他们必须使用安全连接。我正在使用适用于 Visual Studio 2010 的适用于 VB 的 Online Rest 模板,与我在各处看到的普通条目相比,它显着减少了 web.config 中的条目。看起来像这样 :

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <customErrors mode="Off"/>
  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
  </system.webServer>

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

    <bindings>
      <webHttpBinding>
        <binding transferMode="Streamed">
          <security mode="Transport" />
        </binding>
      </webHttpBinding>
    </bindings>

    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
            Configure the WCF REST service base address via the global.asax.vb file and the default endpoint 
            via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" faultExceptionEnabled="true">
          <security mode="Transport" />
        </standardEndpoint>
      </webHttpEndpoint>
    </standardEndpoints>

  </system.serviceModel>



</configuration>

我尝试在此代码之前返回一个 MemoryStream,但他们得到的响应被包装在一个 MemoryStream 元素中并且是 gobbldegook(但这可能是由于我在网络中省略了 transferMode="Streamed" 条目.config。这个版本什么都不返回。

有什么想法吗?

【问题讨论】:

    标签: vb.net wcf rest


    【解决方案1】:

    使用WCFRestContrib,它具有干净且没有任何命名空间的 POX 格式化程序(Plain Old Xml)。

    POX 格式化程序使用 System.Runtime.Serialization.DataContractSerializer,但不同于 WCF REST Contrib xml 格式化程序([Xml Formatter 下讨论) 概述),不序列化数据协定命名空间或 xml 模式 属性,它不需要在 xml 中指定命名空间来 被反序列化并且它不需要元素在特定的 命令。这使您能够提供和接受非常简单的 xml。

    POX Formatter Overview

    【讨论】:

      【解决方案2】:

      使用 Stream 作为返回值。

      【讨论】:

      • 谢谢你,我确实想到了。如果接收者不使用 Microsoft 工具,他是否能够查看流中的文本?
      • @tim 绝对是。您返回的只是一个字节流。不要忘记将 content-type 标头设置为适当的媒体类型。
      • 嗨,Darrel,刚刚收到他们无法读取流的响应。我使用并返回了一个 MemoryStream。这是我的问题吗,如何将内存流转换为普通流?
      • @tim 内存流很好。我一直在使用它们。你能发布一些代码来展示你是如何返回流的吗?
      • 嗨,Darrel,我已经发布了一些代码供您在我的原始帖子中查看。希望这是有道理的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多