考虑下面的.NET ASMX Web服务和两种Web方法。
using System;
using System.Runtime.Remoting.Messaging;
using System.Web.Services;
using System.Xml.Serialization;
namespace DemoWebService
{
public class
ArrayItem
{
public int
Value1;
public int
Value2;
};
[WebService(Namespace = "http://begen.name/xml/namespace/2009/09/demo-web-service/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class DemoService :
WebService
{
[WebMethod]
[return: XmlArray("Items")]
[return: XmlArrayItem("Item")]
public ArrayItem []
GetArray()
{
return BuildArray();
}
[WebMethod]
public IAsyncResult
BeginGetArrayAsync(AsyncCallback callback, object callbackData)
{
BuildArrayDelegate fn = BuildArray;
return fn.BeginInvoke(callback, callbackData);
}
[WebMethod]
[return: XmlArray("Items")]
[return: XmlArrayItem("Item")]
public ArrayItem []
EndGetArrayAsync(IAsyncResult result)
{
BuildArrayDelegate fn = (BuildArrayDelegate)((AsyncResult)result).AsyncDelegate;
return fn.EndInvoke(result);
}
private delegate ArrayItem []
BuildArrayDelegate();
private ArrayItem []
BuildArray()
{
ArrayItem [] retval = new ArrayItem[2];
retval[0] = new ArrayItem();
retval[0].Value1 = 1;
retval[0].Value2 = 2;
retval[1] = new ArrayItem();
retval[1].Value1 = 3;
retval[1].Value2 = 4;
return retval;
}
}
}
GetArray和GetArraySync web方法都返回
ArrayItem
通过将[XmlArray]和[XmlArrayItem]属性应用于
GetArray()
方法,我已经能够控制.Net用于序列化返回值的XML元素,并得到如下结果:
<?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>
<GetArrayResponse xmlns="http://begen.name/xml/namespace/2009/09/demo-web-service/">
<Items>
<Item>
<Value1>int</Value1>
<Value2>int</Value2>
</Item>
<Item>
<Value1>int</Value1>
<Value2>int</Value2>
</Item>
</Items>
</GetArrayResponse>
</soap:Body>
</soap:Envelope>
EndGetArrayAsync()
方法,但它们似乎不会像对同步对象那样影响响应
GetArray()
方法。在本例中,响应如下所示:
<?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>
<GetArrayAsyncResponse xmlns="http://begen.name/xml/namespace/2009/09/demo-web-service/">
<GetArrayAsyncResult>
<ArrayItem>
<Value1>int</Value1>
<Value2>int</Value2>
</ArrayItem>
<ArrayItem>
<Value1>int</Value1>
<Value2>int</Value2>
</ArrayItem>
</GetArrayAsyncResult>
</GetArrayAsyncResponse>
</soap:Body>
</soap:Envelope>
在本例中,返回值的默认XML元素名为“GetArrayAsyncResult”,数组项的XML元素名来自C#类名“ArrayItem”。
有没有办法让异步web方法尊重应用于End*方法返回值的[XmlArray]和[XmlArrayItem]属性?