WebService应该是一个基于Http,Soap的协议栈。底层还是依赖着Http协议(一般来说)。
可以把对象经过XML序列化之后或者是服务A上的XML读进String里,然后返回。这样调用
再把String里的XML反序列化或者读成XML文件就可以了。(当然方法很多)
如果都是.Net调用,而且是一个solution里的工程(两个WebService)那么返回自定义
对象也是可以的。
//WebMethodCollection.asmx.cs
//Method 1: Parameter is a complex type.
[WebMethod]
public string DisplayPersonInfo(ClsCommon.Person someone)
{
return someone.Name + " " + someone.Gender;
}
//Method 2: Return a ArrayList.
[WebMethod]
public ArrayList GetPersonList(int number)
{
ArrayList personList = new ArrayList(number);
for(int i=0; i<number; i++)
{
ClsCommon.Person someone = new ClsCommon.Person();
//Just for test.
someone.Name = "Person" + i.ToString();
someone.Gender = "Male";
personList.Add(someone);
}
return personList;
}
//Method 3: Return a complex type.
[WebMethod]
public ClsCommon.Person GetPersonInstance( string strName, string strGender )
{
ClsCommon.Person someone = new ClsCommon.Person(strName, strGender);
return someone;
}
//Method 4: string ( xml serialization )
[WebMethod]
public string GetSerializedPerson()
{
ClsCommon.Person Jordan = new ClsCommon.Person("Jordan", "Male");
//Serialized the person instance. Return the xml string.
return ClsCommon.SerializeController.XmlSerialize(Jordan);
}
//Method 5: byte[] ( Binary serialization )
[WebMethod]
public byte[] GetBinarySerializedPerson()
{
ClsCommon.Person YaoMing = new ClsCommon.Person("YaoMing", "Male");
//Serialized the person instance. Return the byte array.
return ClsCommon.SerializeController.BinarySerialize(YaoMing);
}
//Method 6: Get the byte array of the excel file.
[WebMethod]
public byte[] GetByteArrayOfExcel()
{
//Just for test
string fileName = "D:\\tmp\\TestExcel\\test.xls";
byte[] bytesData;
using(FileStream fs = File.OpenRead(fileName))
{
bytesData = new byte[(int)fs.Length];
fs.Read(bytesData, 0, bytesData.Length);
fs.Close();
}
return bytesData;
}
_____________________________________________
//ClsCommon.cs 提供序列化和反序列化封装方法。
using System;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
namespace ClsCommon
{
/// <summary>
/// SerializeController の概要の説明です。
/// </summary>
public class SerializeController
{
#region XmlSerialize/XmlDeserialize
public static string XmlSerialize(object obj)
{
XmlSerializer serializer = new XmlSerializer(obj.GetType());
using(MemoryStream stream = new MemoryStream())
{
serializer.Serialize(stream, obj);
stream.Close();
return Encoding.UTF8.GetString(stream.ToArray());
}
}
public static object XmlDeserialize(string xml, Type type)
{
XmlSerializer serializer = new XmlSerializer(type);
using(MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
{
return serializer.Deserialize(stream);
}
}
#endregion
#region BinarySerialize/BinaryDeserialize
public static byte[] BinarySerialize(object obj)
{
BinaryFormatter bf = new BinaryFormatter();
using(MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, obj);
ms.Close();
return ms.ToArray();
}
}
public static object BinaryDeserialize(byte[] bytes)
{
BinaryFormatter bf = new BinaryFormatter();
object obj;
using(MemoryStream ms = new MemoryStream(bytes))
{
obj = bf.Deserialize(ms);
ms.Close();
}
return obj;
}
#endregion
}
}
_________________________________________________
//Person.cs 自定义类型,用作Demo.
using System;
namespace ClsCommon
{
[Serializable]
public class Person
{
public string Name;
public string Gender;
public Person()
{}
public Person(string name, string gender)
{
Name = name;
Gender = gender;
}
}
}
__________________________________________________
// 做了一个WebApplication调用WebService,如果是WebService调用也同理.
// WebForm1.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebMethodCollectionsApp
{
public class WebForm1 : System.Web.UI.Page
{
private MyWebSvc.WebMethodCollections MyWebMethods;
private void Page_Load(object sender, System.EventArgs e)
{
if( !Page.IsPostBack )
{
IniWebService();
InvokeWebMethod1();
Response.Write("<br/>");
InvokeWebMethod2();
Response.Write("<br/>");
InvokeWebMethod3();
Response.Write("<br/>");
InvokeWebMethod4();
Response.Write("<br/>");
InvokeWebMethod5();
Response.Write("<br/>");
//InvokeWebMethod6();
}
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
//Instantiate the web service.
private void IniWebService()
{
MyWebMethods = new MyWebSvc.WebMethodCollections();
}
//Call WebMethod 1: Parameter is a complex type.
private void InvokeWebMethod1()
{
MyWebSvc.Person Peter = new MyWebSvc.Person();
Peter.Name = "Peter";
Peter.Gender = "Male";
Response.Write("Web Method [1] <br/>");
string strResult = MyWebMethods.DisplayPersonInfo(Peter);
Response.Write( strResult );
Response.Write("<br/>");
}
//Call WebMethod 2: Return a ArrayList.
private void InvokeWebMethod2()
{
int number = 3;
object[] objects = MyWebMethods.GetPersonList(number);
Response.Write("Web Method [2] <br/>");
for(int i=0; i<objects.Length; i++)
{
MyWebSvc.Person someone = (MyWebSvc.Person)objects[i];
Response.Write(someone.Name + " " + someone.Gender);
Response.Write("<br/>");
}
}
//Call WebMethod 3: Return a complex type.
private void InvokeWebMethod3()
{
Response.Write("Web Method [3] <br/>");
MyWebSvc.Person someone = MyWebMethods.GetPersonInstance("Susan", "female");
Response.Write( someone.Name + " " + someone.Gender );
Response.Write("<br/>");
}
//Call WebMethod 4: Return a serialized object's xml string.
private void InvokeWebMethod4()
{
Response.Write("Web Method [4] <br/>");
//Get the xml string.
string strXML = MyWebMethods.GetSerializedPerson();
//Deserialize the xml string to object. The object is serializable.
ClsCommon.Person someone =
(ClsCommon.Person)ClsCommon.SerializeController.XmlDeserialize(strXML,typeof(ClsCommon.Person));
Response.Write( someone.Name + " " + someone.Gender );
Response.Write("<br/>");
}
//Call WebMethod 5: Return byte array that be serialized by BinaryFormat.
private void InvokeWebMethod5()
{
Response.Write("Web Method [5] <br/>");
//Get the byte array.
byte[] bytes = MyWebMethods.GetBinarySerializedPerson();
ClsCommon.Person someone =
(ClsCommon.Person)ClsCommon.SerializeController.BinaryDeserialize(bytes);
Response.Write( someone.Name + " " + someone.Gender );
Response.Write("<br/>");
}