加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 07:43:22 所属栏目:百科 来源:网络整理
导读:我正在努力几个小时,找不到通信问题的解决方案. 我的服务需要通过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/以下,并具有如下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfService1
{
    // 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.
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}",value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}

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

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

<CompositeType BoolValue = 0 StringValue = "">

而不是这样:

<CompositeType>
  <BoolValue>0</BoolValue>
  <StringValue></StringValue>
</CompositeType>

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

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

提前感谢你的时间.

这里有很大的更新

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

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

来自客户的消息:

<MESSAGE MessageParam1 = 0>
  <AdditionalInfo>Some message info </AdditionalInfo>
</MESSAGE>

响应:

<RESPONSE ResponseParam1 = 0>
  <AdditionalInfo>Some response info</AdditionalInfo>
</MESSAGE>

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

POST /Service1.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/ShowUser"

<?xml version="1.0" encoding="utf-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/">
  <soap:Body>
    <ShowUser xmlns="http://tempuri.org/">
      <MyLogin>
        <User>string</User>
        <Password>string</Password>
        <Database>string</Database>
      </MyLogin>
    </ShowUser>
  </soap:Body>
</soap:Envelope>

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-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/">
  <soap:Body>
    <ShowUserResponse xmlns="http://tempuri.org/">
      <ShowUserResult>string</ShowUserResult>
    </ShowUserResponse>
  </soap:Body>
</soap:Envelope>

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

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

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

namespace RESTService
{
    // NOTE: If you change the interface name "IService1" here,you must also update the reference to "IService1" in Web.config.

    [ServiceContract]
    [XmlSerializerFormat]
    public interface IService
    {
        [OperationContract]
        [WebGet]
        Response EchoWithGet(string s);

        [OperationContract]
        [WebInvoke]
        Response EchoWithPost(string s);
    }

    public class Response
    {
        [XmlAttribute]
        public int ResponseParam1;
        [XmlElement]
        public string AdditionalInfo;
    }
}

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

这里是实现和配置:

namespace RESTService
{
    // 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.
    public class Service1 : IService
    {
        public Response EchoWithGet(string s)
        {
            Response Transaction;
            Transaction = new Response();
            Transaction.AdditionalInfo = "I've responded: " + s;
            return Transaction;
        }
        public Response EchoWithPost(string s)
        {
            Response Transaction;
            Transaction = new Response();
            Transaction.AdditionalInfo = "I've responded: " + s;
            return Transaction;
        }
    }
}

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

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

无论如何,这里是配置:

<system.serviceModel>
      <services>
         <service name="RESTService.Service1">
           <host>
           </host>
           <endpoint 
               address="rest" behaviorConfiguration="webBehavior"
               bindingConfiguration="" binding="webHttpBinding" 
               contract="RESTService.IService"/>
         </service>
       </services>
       <behaviors>
         <endpointBehaviors>
            <behavior name="webBehavior">
               <webHttp />
            </behavior>
         </endpointBehaviors>
       </behaviors>
</system.serviceModel>

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

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

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

<Response ResponseParam1="0">
<AdditionalInfo>I've responded: test</AdditionalInfo>
</Response>

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

<?xml version="1.0" encoding="utf-8"?><Response ResponseParam1="0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <br/>
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><br/>
<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):

<CompositeType BoolValue = 0 StringValue = "">

rather than this:

<CompositeType>
  <BoolValue>0</BoolValue>
  <StringValue></StringValue>
</CompositeType>

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

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

渣子

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读