【问题标题】:Return sequence of several values in SOAP web service, using asp.NET C#使用 asp.NET C# 在 SOAP Web 服务中返回多个值的序列
【发布时间】:2015-04-19 20:07:15
【问题描述】:

我必须在 ASP.NET 中实现一个 SOAP 1.1 Web 服务。我得到了请求和响应示例以及一个有问题的 wsdl 规范,当将其提供给 wsdl--> 代码向导时,它不会生成给出正确响应的代码。所以我坚持手动修复自动生成的 C# 代码。

这是其中一种方法必须产生的响应:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sws="http://something/WebService">
   <soapenv:Header/>
   <soapenv:Body>
      <sws:MyActionResponse>
         <sws:returnVariableOne>?</sws:returnVariableOne>
         <sws:returnVariableTwo>?</sws:returnVariableTwo>
         <sws:returnVariableThree>?</sws:returnVariableThree>
      </sws:MyActionResponse>
   </soapenv:Body>
</soapenv:Envelope>

我找不到如何让&lt;sws:MyActionResponse&gt; 按指定顺序包含多个元素。

我有这段代码在&lt;sws:MyActionResponse&gt; 元素下只产生一个子元素:

[System.Web.Services.WebMethodAttribute()]
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://something/WebService/MyAction", RequestElementName="MyActionRequest", RequestNamespace="http://something/WebService", ResponseNamespace="http://something/WebService", ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped, Use=System.Web.Services.Description.SoapBindingUse.Literal)]
[return: System.Xml.Serialization.XmlElementAttribute("returnVariableOne")]
public override string MyAction(string inputVariable)
{
    return "Value of variable #1";
}

它的响应 xml 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <MyActionResponse xmlns="http://something/WebService">
      <returnVariableOne>Value of variable #1</returnVariableOne>
    </MyActionResponse>
  </soap:Body>
</soap:Envelope>

嗯,我需要三个子元素,所以我正在寻找 C# 语法,它可以让 WebMethod 按规定的顺序返回几个元素的序列。我知道我可以返回一个包含复杂数据结构的复杂元素,但这无济于事,因为我必须匹配规范中给出的 xml 响应示例。

【问题讨论】:

  • 为什么不把 Response 本身当作一个复杂的类型呢?
  • 也许,但我不知道应该使用哪些属性和语法来覆盖 SOAP 响应消息元素。
  • 我回顾了我过去使用的一项服务 - 示例响应的定义如下所示:pastebin.com/iAREa2Bg。为什么不附上 WSDL 以查看它有什么问题并修复它 - 它可能更容易?
  • 抱歉,wsdl 不能分享,因为它涉及到商业应用,所以它是巨大的和机密的。
  • 我明白了。没问题,很高兴你以某种方式解决了它。

标签: asp.net web-services soap


【解决方案1】:

我已经用非常残酷的方法解决了这个问题 - 通过编辑输出流。直到我知道更好的方法。

将此代码放在 global.asax 中:

using System.IO;
using System.Text.RegularExpressions;
using System.Text;

public class XmlElementStripper : MemoryStream
{
    private Stream outputStream;
    private Regex reStripper = new Regex(@"</?removeThisTag>", RegexOptions.Compiled | RegexOptions.Multiline);

    public XmlElementStripper(Stream output)
    {
        outputStream = output;
    }
    public override void Write(Byte[] buffer, int offset, int count)
    {
        // Convert the content in buffer to a string
        String contentInBuffer = UTF8Encoding.UTF8.GetString(buffer);
        // Strip out the tags
        contentInBuffer = reStripper.Replace(contentInBuffer, String.Empty);
        // Output the modified string
        outputStream.Write(UTF8Encoding.UTF8.GetBytes(contentInBuffer), offset, UTF8Encoding.UTF8.GetByteCount(contentInBuffer));
    }
}

在全局类(System.Web.HttpApplication)中:

protected void Application_PostReleaseRequestState(Object sender, EventArgs e)
{
    if (Response.ContentType.StartsWith("text/xml"))
    {
        Response.Filter = new XmlElementStripper (Response.Filter);
    }
}

现在,如果 web 方法有这个返回属性

[return: System.Xml.Serialization.XmlElementAttribute("removeThisTag")]

然后从输出流中编辑和标记,当 Web 方法返回由多个 xml 序列化字段组成的复杂类型时,它们是主 SOAP 响应消息元素的直接子元素。

【讨论】:

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