void dosomethingtest()
{
localhost.service service = new windowsapp.localhost.service();
service.helloworldcompleted += new windowsapp.localhost.helloworldcompletedeventhandler(service_helloworldcompleted);
// do asyn calling here
service.helloworldasync();
}
void service_helloworldcompleted(object sender, windowsapp.localhost.helloworldcompletedeventargs e)
{
if (e.error == null)
{
messagebox.show(e.result);
}
else
{
messagebox.show(e.error.message);
}
}
服務(wù)器端代碼
[webservice(namespace = "http://tempuri.org/")]
[webservicebinding(conformsto = wsiprofiles.basicprofile1_1)]
public class service : system.web.services.webservice
{ public service () {
}
[webmethod] public string helloworld() {
return "hello world";
}
}
很簡單,沒有了asynccallback、iasyncresult 這兩個煩人的東西,調(diào)用的代碼變得簡潔、優(yōu)雅了,而且可以從e.result得到強(qiáng)類型的返回值(上例為"hello world")。但是,有興趣的話,可以看看vs 2005生成的referance.cs文件,那可比2003中的復(fù)雜很多。其中可以看到system.componentmodel.asynccompletedeventargs 、 system.threading.sendorpostcallback(delegate)這兩個在 .net 1.x 中沒有的“怪物”,估計(jì)用到的地方還不止webservice客戶端。
新聞熱點(diǎn)
疑難解答
圖片精選