第一,方法的粒度,由于要通过远程访问,方法的粒度过小,会使webservice的调用很频繁,增加网络的负载
如
[WebMethod]
public int GetAccountID(Guid PassportID, out Guid AccountID)

{
Core.VirtualBank vb = new Core.VirtualBank();
return vb.GetAccountID( PassportID, out AccountID);

}
[WebMethod]
public int GetPassportID(string username, out Guid PassportID)

{
Core.VirtualBank vb = new Core.VirtualBank();
return vb.GetPassportID( username, out PassportID);

}
[WebMethod]
public int GetAccountIDFromName(string username, out Guid AccountID)

{
Core.VirtualBank vb = new Core.VirtualBank();
return vb.GetAccountID(username, out AccountID);

}
调用频繁,但是客户用起来比较方便
二,粒度大。一次传多的数据。客户端再封装一次service
如:服务端
[WebMethod]
public ShoppingCart[] AllCart(string UserID)

{
ShopCart sc = new ShopCart(UserID);
return sc.Items;
}
[WebMethod]
public Product[] GetProductes()

{
Producte p = new Producte();
return p.GetProductes();
}
[WebMethod]
public Category[] GetCategories()

{
Producte p = new Producte();
return p.GetCategories();
}
其中每个Service都返回一个类型的数组,包含了大量的数据
客户端可以自己封装取回的数据
如:客户端
public static ArrayList GetCategories()

{
if ( HttpContext.Current.Cache["Categories"] == null )

{
HttpContext.Current.Cache["Categories"] = GetCategoriesFromDB();
}
return (ArrayList)HttpContext.Current.Cache["Categories"];
}

public static string GetCategoryDescription( int intCategory )

{
string description = null;
if ( HttpContext.Current.Cache["Categories1"] == null )

{
HttpContext.Current.Cache["Categories"] = GetCategoriesFromDB();
}
ArrayList al = (ArrayList)HttpContext.Current.Cache["Categories"];
for(int i=0;i<al.Count;i++)

{
etpweb.Etp.Category c = (etpweb.Etp.Category)al[i];
if(c.Id == intCategory)

{
description = c.Description;
return description;
}

}
return description;
}

private static ArrayList GetCategoriesFromDB()

{
etpweb.Etp.BubbleService bs = new etpweb.Etp.BubbleService();
ArrayList al = new ArrayList( bs.GetCategories());
return al;
}

客户端取回服务器端的数据后放入缓存,再加上每次取回的数据比较多,对服务器的访问就比较少。
总结:
如果客户端需要很多和服务器的交互,应该提供第二种方式,如果只是调用一个简单的方法,应该用第一种方式
所有服务器端最好提供两种实现
第二种实现比较复杂
因为数据可能会绑定到Datagrid等,Service的返回结果可以用
1。IList,实现起来简单,直接用NHIbernate的Qury.list,只是不是强类型,客户端要做转换
如果没有用到过类型如:SnInfo,webservice的代理类不会生成SnInfo类的代理.要手动修改代理类
即Reference.cs
如:服务器:
[WebMethod]
public IList QuerySnInfoByOem(string type,string Oem)

{
return _sn.QuerySnInfo(Type.GetType(type),Oem);
}
修改:Reference.cs
加上:
public class SNInfo

{

Member Variables#region Member Variables
protected Guid _id;
protected Guid _passportID;
protected bool _availability;
protected bool _deleted;

protected int _money;
protected DateTime _date;
protected int _ln;
protected string _oEM;
protected string _business;
protected DateTime _availableDate;
protected string _sn;
#endregion


Constructors#region Constructors


public SNInfo()
{ }

public SNInfo( Guid passportID, bool availability, int money, DateTime date, int ln, string oEM, string business, DateTime availableDate, string sn )

{
this._passportID = passportID;
this._availability = availability;
this._money = money;
this._date = date;
this._ln = ln;
this._oEM = oEM;
this._business = business;
this._availableDate = availableDate;
this._sn = sn;
}

#endregion


Public Properties#region Public Properties

public Guid Id

{

get
{return _id;}

set
{_id = value;}
}

public Guid PassportID

{

get
{ return _passportID; }

set
{ _passportID = value; }
}

public bool Availability

{

get
{ return _availability; }

set
{ _availability = value; }
}
public bool Deleted

{

get
{ return _deleted; }

set
{ _deleted = value; }
}


public int Money

{

get
{ return _money; }

set
{ _money = value; }
}

public DateTime Date

{

get
{ return _date; }

set
{ _date = value; }
}

public int Ln

{

get
{ return _ln; }

set
{ _ln = value; }
}

public string OEM

{

get
{ return _oEM; }
set

{
if ( value != null && value.Length > 50)
throw new ArgumentOutOfRangeException("Invalid value for _oEM", value, value.ToString());
_oEM = value;
}
}
public string CardID

{
get

{
return OEM+Business+Ln;
}
}
public string Business

{

get
{ return _business; }
set

{
if ( value != null && value.Length > 50)
throw new ArgumentOutOfRangeException("Invalid value for _business", value, value.ToString());
_business = value; }
}

public DateTime AvailableDate

{

get
{ return _availableDate; }
set

{
_availableDate = value;
}
}

public string Sn

{

get
{ return _sn; }
set

{
if ( value != null && value.Length > 16)
throw new ArgumentOutOfRangeException("Invalid value for Sn", value, value.ToString());
_sn = value;
}
}

#endregion
}

2.用类型数组,方便也很简单
服务器端返回类型数组,这样会自动生产代理,也是强类型,需要增加一些属性时,只要修改类型就可以了。
3 用类,其中类的成员是Ilist(ArrayList等集合类型)或者类型数组
优点,客户端使用方便
如:服务器端定义了Products的类,包含Arraylist的 products属性
客户端可以通过Products.products访问