使用asp.NET C#返回SOAP Web服务中几个值的顺序 [英] Return sequence of several values in SOAP web service, using asp.NET C#
问题描述
我必须在ASP.NET中实现SOAP 1.1 Web服务.我得到了请求和响应示例以及一个错误的wsdl规范,当将其馈送到wsdl-> code向导时,不会生成给出正确响应的代码.因此,我不得不手动修复自动生成的C#代码.
I have to implement a SOAP 1.1 web service in ASP.NET. I am given request and response examples and a glitchy wsdl spec that, when fed to the wsdl-->code wizard, doesn't produce code that gives correct response. So I am stuck with manual fixing of the auto-generated C# code.
以下是方法必须产生的响应:
Here is the response one of the methods must produce:
<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>
我找不到一种方法来使< sws:MyActionResponse>
包含指定顺序的多个元素.
I can't find a way how to make the <sws:MyActionResponse>
contain several elements, in the specified order.
我有这段代码仅在< sws:MyActionResponse>
元素下产生一个孩子:
I have this code that produces only one child under the <sws:MyActionResponse>
element:
[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看起来像这样:
The response xml from it looks like this:
<?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响应样本.
Well, I need three child elements there, so I am looking for the C# syntax that causes the WebMethod to return sequence of several elements in prescribed order. I am aware that I could return one complex element with complex data structures inside it, but that doesn't help because I have to match the xml response sample I am given in the spec.
推荐答案
我已经通过相当残酷的方法解决了这一问题-通过编辑输出流.直到我了解更好的方法.
I have solved this with pretty brutal approach - by editing the output stream. Until I learn of better way.
将此代码放在global.asax中:
Put this code in 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)中:
In Global class (System.Web.HttpApplication):
protected void Application_PostReleaseRequestState(Object sender, EventArgs e)
{
if (Response.ContentType.StartsWith("text/xml"))
{
Response.Filter = new XmlElementStripper (Response.Filter);
}
}
现在,如果Web方法具有此返回属性
Now, if the web method has this return attribute
[return: System.Xml.Serialization.XmlElementAttribute("removeThisTag")]
然后从输出流中编辑and标记,并且当web方法返回由多个xml序列化字段组成的复杂类型时,它们是SOAP响应消息主元素的直接子代.
Then the and tags are edited out of the output stream, and when the web method returns complex type consisting of several xml-serialized fields, they are direct children of the main SOAP response message element.
这篇关于使用asp.NET C#返回SOAP Web服务中几个值的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!