【问题标题】:How to return an xml response from a WCF Service如何从 WCF 服务返回 xml 响应
【发布时间】:2018-07-16 20:40:19
【问题描述】:

我不熟悉 WCF 服务,所以我很难将 XmlElement 作为返回类型返回。

我收到来自WCF Test Client 的消息(在调试模式下运行):

wcf 测试客户端不支持该操作,因为它使用 XmlElement 类型。

[ServiceContract]
public interface IClientService
{

    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Xml)]
    XmlElement GetClientXml(int value);

}

namespace testWCF
{
    public class testSvc: IClientService
    {

        public XmlElement GetClientXml(int value)
        {
            string appDir = AppContext.BaseDirectory;
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(appDir + @"Xml\ResponseTempl.xml");
            return xDoc.DocumentElement;
        }

    }
}

我也提到过这个,但它可能太旧了,因为我使用的是 4.6.1 框架:Returning XML From a WCF Service

我的Web.Debug.config 文件:

<?xml version="1.0" encoding="utf-8"?>

<!-- For more information on using web.config transformation visit https://go.microsoft.com/fwlink/?LinkId=125889 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.web>    
  </system.web>

  <system.serviceModel>
    <services>
      <service name="AucklandRegionalPatientWCF.PatientDemographicService" >

        <!-- these endpoint are necessary to return SOAP service -->
        <endpoint address=""
                     binding="basicHttpBinding"
                     contract="AucklandRegionalPatientWCF.IPatientDemographicService" />
        <endpoint address="mex"
                  contract="IMetadataExchange" binding="mexHttpBinding"/>

        <!-- REST service return xml -->
        <!--To call this endpoint use: [service].svc/xml/[method_Name]-->
        <endpoint address="xml"
                  binding="webHttpBinding" behaviorConfiguration="xmlBehavior"
                  contract="AucklandRegionalPatientWCF.IPatientDemographicService" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          
          <serviceMetadata httpGetEnabled="true"/>
          
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>

      <endpointBehaviors>
        <!-- use XML serialization -->
        <behavior name="xmlBehavior">
          <webHttp/>          
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
</configuration>

【问题讨论】:

  • 从您分享的代码中并不完全清楚发生了什么。您有一个接口IClientService,然后是一个实现或继承自TestSvc 的类。 “测试客户端”的代码在哪里?
  • @MikeMcCaughan - 我在 vs2017 中运行调试模式,通过 WCF 测试客户端接口测试服务。
  • 错误信息很清楚。 WCF TestClient 不支持XmlElement。您需要编写自己的测试工具(例如控制台应用程序)来测试它。有关 WCF 测试客户端不支持的内容的列表,请参阅 stackoverflow.com/a/8568078/745969
  • 好的,我现在明白了,是测试客户端本身不支持xml。

标签: wcf wcf-data-services


【解决方案1】:

从 WCf 获取 XML 元素作为响应的最佳方式是使用 XML 格式的字符串。性能最佳且重量轻..... 创建响应类并使用 XML 序列化....会有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    相关资源
    最近更新 更多