如何从WCF服务返回xml响应

时间:2018-02-06 17:38:17

标签: wcf wcf-data-services

我不能流利使用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>
&#13;
&#13;
&#13;

WCF Test Client

1 个答案:

答案 0 :(得分:1)

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