WCF服务,在SOAP或纯XML中的响应,怎么样?

前端之家收集整理的这篇文章主要介绍了WCF服务,在SOAP或纯XML中的响应,怎么样?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在努力几个小时,找不到通信问题的解决方案.
我的服务需要通过SOAP或纯 XML与客户端进行通信

我的服务将基于WCF框架编写,但是我的客户端不是.

你可以逐步告诉我如何更改我的服务代码和配置,以便它将返回SOAP或XML消息?我对这两种解决方案感兴趣

我试图通过这个答案来实现这一点:

REST / SOAP endpoints for a WCF service
Call WCF Using SOAP call

但是这个解决方案没有一个适合我的工作.我的浏览器中没有任何东西或找不到该资源的错误.

所以我开始了一个新的WCF服务项目.它运行在http://localhost:3151/以下,并具有如下代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.ServiceModel;
  6. using System.Text;
  7.  
  8. namespace WcfService1
  9. {
  10. // NOTE: If you change the class name "Service1" here,you must also update the reference to "Service1" in Web.config and in the associated .svc file.
  11. public class Service1 : IService1
  12. {
  13. public string GetData(int value)
  14. {
  15. return string.Format("You entered: {0}",value);
  16. }
  17.  
  18. public CompositeType GetDataUsingDataContract(CompositeType composite)
  19. {
  20. if (composite.BoolValue)
  21. {
  22. composite.StringValue += "Suffix";
  23. }
  24. return composite;
  25. }
  26. }
  27. }

我可能需要创建两个端点?他们都应该包含basicHttpBinding作为绑定参数.但还有什么?

另外,如何生成这样的XML(基于数据合约):

  1. <CompositeType BoolValue = 0 StringValue = "">

而不是这样:

  1. <CompositeType>
  2. <BoolValue>0</BoolValue>
  3. <StringValue></StringValue>
  4. </CompositeType>

请注意,我需要作为双向通信,所以我的服务需要接收SOAP或XML以及SOAP或XML中的响应.

另外,如果这是可能的,我想看到我的浏览器中的服务方法的描述就像在ASP.NET.

提前感谢你的时间.

这里有很大的更新

我试图找到一些解决方案,这里是我收集到目前为止的一切.我只专注于纯XML.

让我们说协议看起来像这样:

来自客户的消息:

  1. <MESSAGE MessageParam1 = 0>
  2. <AdditionalInfo>Some message info </AdditionalInfo>
  3. </MESSAGE>

响应:

  1. <RESPONSE ResponseParam1 = 0>
  2. <AdditionalInfo>Some response info</AdditionalInfo>
  3. </MESSAGE>

首先,我想看到当前服务方式的描述,并且它们将被发送/接收的确切格式.我的意思是当我在浏览器中调用了Service(刚启动的服务)时,我尝试了ASP .NET 2.0 WebServices,我有这样的回应:

  1. POST /Service1.asmx HTTP/1.1
  2. Host: localhost
  3. Content-Type: text/xml; charset=utf-8
  4. Content-Length: length
  5. SOAPAction: "http://tempuri.org/ShowUser"
  6.  
  7. <?xml version="1.0" encoding="utf-8"?>
  8. <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  9. <soap:Body>
  10. <ShowUser xmlns="http://tempuri.org/">
  11. <MyLogin>
  12. <User>string</User>
  13. <Password>string</Password>
  14. <Database>string</Database>
  15. </MyLogin>
  16. </ShowUser>
  17. </soap:Body>
  18. </soap:Envelope>
  19.  
  20. HTTP/1.1 200 OK
  21. Content-Type: text/xml; charset=utf-8
  22. Content-Length: length
  23.  
  24. <?xml version="1.0" encoding="utf-8"?>
  25. <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  26. <soap:Body>
  27. <ShowUserResponse xmlns="http://tempuri.org/">
  28. <ShowUserResult>string</ShowUserResult>
  29. </ShowUserResponse>
  30. </soap:Body>
  31. </soap:Envelope>

这是伟大的除此之外,它还包含SOAP信封(底层数据也不同),但重要的是我可以向客户端显示它,并告诉他:“您正在使用这样的XML结构进行HTTP POST方法,并且您收到这样的答案”.我想要与WCF类似的结果,但是更简单 – 没有SOAP.到目前为止,当我点击Service1.svc我收到一堆我不想要的WSDL代码.我知道这对某些情况来说非常棒,但是我不知道什么平台正在使用我的客户端.如果他不会使用.NET,那么它可能无法正确导入WSDL.

所以你知道我想要得到什么.

这是我两天过后所做的事情:

  1. namespace RESTService
  2. {
  3. // NOTE: If you change the interface name "IService1" here,you must also update the reference to "IService1" in Web.config.
  4.  
  5. [ServiceContract]
  6. [XmlSerializerFormat]
  7. public interface IService
  8. {
  9. [OperationContract]
  10. [WebGet]
  11. Response EchoWithGet(string s);
  12.  
  13. [OperationContract]
  14. [WebInvoke]
  15. Response EchoWithPost(string s);
  16. }
  17.  
  18. public class Response
  19. {
  20. [XmlAttribute]
  21. public int ResponseParam1;
  22. [XmlElement]
  23. public string AdditionalInfo;
  24. }
  25. }

如你所见,我没有提供Message类,因为我不知道如何通过浏览器将其传递给方法.我只知道如何传递一个字符串.

这里是实现和配置:

  1. namespace RESTService
  2. {
  3. // NOTE: If you change the class name "Service1" here,you must also update the reference to "Service1" in Web.config and in the associated .svc file.
  4. public class Service1 : IService
  5. {
  6. public Response EchoWithGet(string s)
  7. {
  8. Response Transaction;
  9. Transaction = new Response();
  10. Transaction.AdditionalInfo = "I've responded: " + s;
  11. return Transaction;
  12. }
  13. public Response EchoWithPost(string s)
  14. {
  15. Response Transaction;
  16. Transaction = new Response();
  17. Transaction.AdditionalInfo = "I've responded: " + s;
  18. return Transaction;
  19. }
  20. }
  21. }

配置对我来说也是一个谜.我试图创建绑定,就像在MSDN:http://msdn.microsoft.com/en-us/library/aa395208.aspx主题之一所描述的那样

但是在调用了一个方法之后,我已经返回了类似SOAP的故障结构,但没有SOAP信封.

无论如何,这里是配置:

  1. <system.serviceModel>
  2. <services>
  3. <service name="RESTService.Service1">
  4. <host>
  5. </host>
  6. <endpoint
  7. address="rest" behaviorConfiguration="webBehavior"
  8. bindingConfiguration="" binding="webHttpBinding"
  9. contract="RESTService.IService"/>
  10. </service>
  11. </services>
  12. <behaviors>
  13. <endpointBehaviors>
  14. <behavior name="webBehavior">
  15. <webHttp />
  16. </behavior>
  17. </endpointBehaviors>
  18. </behaviors>
  19. </system.serviceModel>

当我在浏览器中调用服务方法时,如下所示:

http://localhost:2443/Service1.svc/rest/EchoWithGet?s=test

我的浏览器中有一个XML文件

  1. <Response ResponseParam1="0">
  2. <AdditionalInfo>I've responded: test</AdditionalInfo>
  3. </Response>

所以看起来我在正确的轨道上?然而,我仍然不知道如何收到一个完整的信息(如ASP.NET 2.0中的一个)关于发送和接收的内容.我想不要用嗅探器听服务端口,看看HTTP是怎么回事.我想看看另外,当我看到接收到的XML的源文件时,我看到:

  1. <?xml version="1.0" encoding="utf-8"?><Response ResponseParam1="0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <br/>
  2. xmlns:xsd="http://www.w3.org/2001/XMLSchema"><br/>
  3. <AdditionalInfo>I've responded: test</AdditionalInfo></Response>

我不想在返回的XML中有这个附加信息,如何剪切这个?

另一个问题是第二种方法

http://localhost:2443/Service1.svc/rest/EchoWithPost?s=test

这不按预期工作.我收到错误信息“不允许的方法”.如何解决这个问题?

所以,我们总结一下这个长期的问题.

我有一半的解决方案可能是正确的方式或是错误的方式.我想请你完成它也可以:

>以方法接受XML文件作为参数.
>告诉我如何使用XML作为参数在Web浏览器中调用方法.是的,我不想在.NET中单独的客户端.让浏览器成为客户端.
>告诉我如何在返回的XML中减少不必要的膨胀信息.
>告诉我如何获取.NET 2.0类似的服务描述,同时考虑到我不想要SOAP.
>解释EchoWithPost方法有什么问题.为什么不起作用?

如果我的例子不好,请给我一些基本的例子,如何实现.

我只是在学习.NET和WCF,我很困惑.所有这些问题只有设置正确的数据交换协议…甚至没有statred写一个真正的服务:|

非常感谢您的时间和未来的帮助.

您在此处的服务将返回SOAP消息 – 其中将包含“CompositeType”作为其“payload”.

WCF默认使用SOAP – 任何basicHttpBinding,wsHttpBinding,netTcpBinding等都以SOAP为基础.

如果要返回直接的XML,则需要检查WCF的REST功能 – 这与webHttpBinding(和只有该绑定)一起使用.

Also,how to produce an XML like this (based on data contract):

  1. <CompositeType BoolValue = 0 StringValue = "">

rather than this:

  1. <CompositeType>
  2. <BoolValue>0</BoolValue>
  3. <StringValue></StringValue>
  4. </CompositeType>

这是WCF DataContract序列化程序的一个限制.出于性能原因,它不支持属性,例如您无法创建所需的XML的第一个片段.

如果您绝对必须拥有第一个XML,则需要使用XmlSerializer(具有自己的限制/问题).

渣子

更新:如果你只想返回给定的XML,那么你可能更喜欢REST方法.

查看Pluralsight网站的一个很好的series of screencasts on using REST,特别是一个关于如何创建一个plain-old XML (POX) REST service的截屏.

猜你在找的XML相关文章