Reference and Exception in WebServices

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

WebService是个好东西,这样可以利用远程的WebMethod,而且就像使用本地的方法一样的简单。

呵呵,那我们就直接开始吧。

首先我们建一个WebService Solution,我选的是C#,dotNET的母语。

这样一个框架就生成了,我们首先做的是写一个函数,返回一些数据。

打开SQL Server,你可以看到NorthWind这个数据库的,你有SQL Server就有这个数据库的,但是你删

了就不能找我要了,呵呵。

写一段访问Products这个表,并返回一个DataSet的函数。

首先:

1using System.Data;

2using System.Data.SqlClient;

下面就开始写我们的业务逻辑。

1[WebMethod]

2public DataSet GetProductsDetail(string sqltxt)

3{

4

5 SqlConnection sqlconn = new SqlConnection("server=127.0.0.1;database=NorthWind;uid=sa;pwd=sqlsvrpwd");

6 DataSet ds = new DataSet();

7 SqlDataAdapter sdater = new SqlDataAdapter(sqltxt, sqlconn);

8

9 try

10 {

11 sqlconn.Open();

12 sdater.Fill(ds);

13

14 }

15 catch(Exception ex_1)

16 {

17 throw ex_1;

18 }

19 finally

20 {

21 sqlconn.Close();

22 }

23 return ds;

24}

看到那个[WebMethod]了么?那个就是表示这个函数是可调用的。就像我们在dll中的DLLEXPORT

或者是 __decspec 一样。

现在测试这个WebMethod, 你可以在那个参数中输入“Select Top 1 * from Products“ ,你就可以得到这样的

输出:

<?xml version="1.0" encoding="utf-8" ?>

- <DataSet xmlns="http://tempuri.org/">

- <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

- <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:Locale="zh-CN">

- <xs:complexType>

- <xs:choice maxOccurs="unbounded">

- <xs:element name="Table">

- <xs:complexType>

- <xs:sequence>

<xs:element name="ProductID" type="xs:int" minOccurs="0" />

<xs:element name="ProductName" type="xs:string" minOccurs="0" />

<xs:element name="SupplierID" type="xs:int" minOccurs="0" />

<xs:element name="CategoryID" type="xs:int" minOccurs="0" />

<xs:element name="QuantityPerUnit" type="xs:string" minOccurs="0" />

<xs:element name="UnitPrice" type="xs:decimal" minOccurs="0" />

<xs:element name="UnitsInStock" type="xs:short" minOccurs="0" />

<xs:element name="UnitsOnOrder" type="xs:short" minOccurs="0" />

<xs:element name="ReorderLevel" type="xs:short" minOccurs="0" />

<xs:element name="Discontinued" type="xs:boolean" minOccurs="0" />

</xs:sequence>

</xs:complexType>

</xs:element>

</xs:choice>

</xs:complexType>

</xs:element>

</xs:schema>

- <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">

- <NewDataSet xmlns="">

- <Table diffgr:id="Table1" msdata:rowOrder="0">

<ProductID>1</ProductID>

<ProductName>Chai</ProductName>

<SupplierID>1</SupplierID>

<CategoryID>1</CategoryID>

<QuantityPerUnit>10 boxes x 20 bags</QuantityPerUnit>

<UnitPrice>18.0000</UnitPrice>

<UnitsInStock>39</UnitsInStock>

<UnitsOnOrder>0</UnitsOnOrder>

<ReorderLevel>10</ReorderLevel>

<Discontinued>false</Discontinued>

</Table>

</NewDataSet>

</diffgr:diffgram>

</DataSet>

大家可以看到,这个DataSet是用XML序列化并描述的,这样就起到了跨平台的效果。

但是我们就是这样使用WebService吗?绝对不是!那个页面只是为了我们测试才生成的。

我说过,我们可在程序中像调用本地APIs或者是其他的类库一样调用它。

新建一个工程Windows Application,添加Web Reference,

在 “添加Web引用”对话框的地址中输入你的Web Service描述地址:

http://localhost/WebSvr/Helo.asmx

你就可以看到你刚才的WebMethod : GetProductsDetail

引用这个Web Service,她就以namespace的形式在你的工程中存在。

在你的Form中加TextBox, DataGrid, Button各一个。写代码如下:

1private void button1_Click(object sender, System.EventArgs e)

2{

3 string txt = textBox1.Text.Trim();

4 localhost_WebSvr.Helo helo = new TestWebsrv.localhost_WebSvr.Helo();

5 try

6 {

7 dataGrid1.DataSource = helo.GetProductsDetail(txt).Tables[0].DefaultView;

8 }

9 catch(Exception ex_2)

10 {

11 MessageBox.Show(ex_2.Message);

12 }

13}

14

如此简单的代码,你就能使用WebService,运行程序,你就能看到这个效果:

是不是很神奇?更神奇在后面呢!

现在我们在TextBox中输入“Select * from Product”,这个有什么不同呢?呵呵,就是那个 Product表不存在。

我们点击按钮,结果看到了:

大家知道,WebService运行在远程的服务器上,能给我们数据,这不奇怪。但是它居然能返回Exception!太性感了!

再加一个WebMethod,代码如下:

1[WebMethod]

2public void TestRef( ref string szName )

3{

4 szName = "Helo";

5}

在测试程序中加入以下代码:

1private void button2_Click(object sender, System.EventArgs e)

2{

3 localhost_WebSvr.Helo helo = new TestWebsrv.localhost_WebSvr.Helo();

4 string str = "This is the form string";

5 MessageBox.Show( str + "\r\n Press OK to invoke WebService");

6 helo.TestRef(ref str);

7 MessageBox.Show( "Has the string changed ? : " + str);

8}

最后一个MessageBox居然是: Has the string changed ? : Helo

就是说,你可以给一个Web Service传递一个Reference!

这就是说,你使用WebService时,和你使用其他的类库在使用方式基本上没有区别。她是一个神奇而可爱的东西。

有这样一句名言:

当你不知道你在使用COM时,COM才是成功的。

那么, 当你不知道你在使用Web Service时,Web Service才是成功的。

Web Service是如此的美妙,她一定会是成功的。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航