日期:2011-04-11  浏览次数:20508 次

为了体现XMLWebService的可跨越异质平台、与系统无关与设备无关与消费者的应用程序类型无关等特性。
给学生演示了用C#编写一个Windows程序来消费该服务的例子:

代码很简单,只是添加如下几行而已:
private void button1_Click(object sender, System.EventArgs e)
{
Get.Service1 AA=new WS03.Get.Service1();
string score=AA.GetScore(comboBox1.SelectedItem.ToString(),comboBox2.SelectedItem.ToString()).ToString();
label3.Text="学生"+comboBox1.SelectedItem.ToString()+"的"+comboBox2.SelectedItem.ToString()+"成绩是:"+score;
}

private void button2_Click(object sender, System.EventArgs e)
{
Get.Service1 AA=new WS03.Get.Service1();
string average=AA.GetAverage(comboBox1.SelectedItem.ToString()).ToString();
label4.Text="学生"+comboBox1.SelectedItem.ToString()+"的平均分是:"+average;
}

private void button3_Click(object sender, System.EventArgs e)
{
Get.Service1 AA=new WS03.Get.Service1();
string order=AA.GetOrder(comboBox1.SelectedItem.ToString()).ToString();
label5.Text="学生"+comboBox1.SelectedItem.ToString()+"的排名是:第"+order+"名";
}


然后再用VB.NET编写一个ASP.NET程序来调用该服务:

代码也是非常的简单:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim AA As New [Get].Service1
Label1.Text = AA.GetScore(DropDownList1.SelectedItem.Text, DropDownList2.SelectedItem.Text)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim AA As New [Get].Service1
Label2.Text = AA.GetAverage(DropDownList1.SelectedItem.Text)
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim AA As New [Get].Service1
Label3.Text = AA.GetOrder(DropDownList1.SelectedItem.Text)
End Sub
当然在这两个程序里都不要忘记添加进该Web引用,在本地生成SOAP代理,只不过有很多的工作都由VS.NET这个IDE给完成了。
如果是使用WebMatrix来编写ASP.NET消费Web服务的话,可就有一番工作需要你自己完成了,这个等下次再讲。