首页 >> 读书频道 >> 电脑 >> 一个简单的Web Service案例
 
· 用Visual C#打造
· C#编程实现Excel文
· C#中利用mediapl
· Web service简
· Visual C#编程入
· Visual C#编程入
· Visual C# 3.
· 利用Visual C#实
· 对C# 2.0中匿名方法
· 会员系统例子:
· Visual C#常用函
· 基于FORM的验证
 
· 电视剧《刁蛮公主》下载和
· 美容养颜手册
· 加油金顺剧情分集介绍完整
· 红楼梦:120回全本[清
· 素质教育在美国
· 全唐诗卷四十六
· 韩剧《布拉格恋人》剧情介
· 夜航船[作者:明·张岱]
· 局外人[作者:韩·可爱淘
· 现场流行病学
· 源氏物语[日本:紫式部]
· 武林寓言故事
 
· (出租)中动商场部分及写
· (出租)中动动漫基地&#
· 喜剧学院
· 《善德女王》剧情介绍
· 魔女18号 剧情
· 丑女无敌剧情介绍
· 魔女幼熙剧情介绍
· 龙游天下剧情介绍
· 震撼世界的七日剧情介绍
· 静静的白桦林剧情介绍
· 心情日记—老公今天我想对
· 旗舰剧情介绍
欢迎来到月影社区!如果您觉得这里不错,请推荐给您的朋友们。月影社区:http://wf66.com/

一个简单的Web Service案例


查看有无更新版本

关键字:ASP.NET 2006-9-20

 

在这个例子中,我们将定义一个mathservice类,来对两个数字分别进行加,减,乘,除。当然这个类需要从基类web service中继承。请先看该程序的源代码:
源文件:webservice\math.asmx

<%@ WebService Language="VB" Class="MathService" %>
Imports System
Imports System.Web.Services
Public Class MathService : Inherits WebService
   Public Function <WebMethod()> Add(A As Integer, B As Integer) As Integer
       Return A + B
   End Function
   Public Function <WebMethod()> Subtract(A As Integer, B As Integer) As Integer

       Return A - B
   End Function
   Public Function <WebMethod()> Multiply(A As Integer, B As Integer) As Integer
       Return A * B
   End Function
   Public Function <WebMethod()> Divide(A As Integer, B As Integer) As Integer
       If B = 0
          Return -1
       End If
       Return CInt(A / B)
   End Function


我们还是举“加”,“减”,“乘”,“除”的例子。

我们先创建一个文件用于客户端的用户浏览:
<%@ Import Namespace="MathServiceSpace" %>
<html>
<script language="VB" runat="server">
  Dim Op1 As Integer = 0
  Dim Op2 As Integer = 0
  Public Sub Submit_Click(Sender As Object, E As EventArgs)
      Try
        Op1 = Int32.Parse(Operand1.Text)
        Op2 = Int32.Parse(Operand2.Text)
      Catch Exp As Exception
          ' Ignored
      End Try

      Dim Service As MathService = New MathService()
      Select (CType(sender,Control).ID)
        Case "Add" :
            Result.Text = "<b>Result</b> = " & Service.Add(Op1, Op2).ToString()
        Case "Subtract" :
            Result.Text = "<b>Result</b> = " & Service.Subtract(Op1, Op2).ToString()
        Case "Multiply" :
            Result.Text = "<b>Result</b> = " & Service.Multiply(Op1, Op2).ToString()
        Case "Divide" :
            Result.Text = "<b>Result</b> = " & Service.Divide(Op1, Op2).ToString()
      End Select
  End Sub
</script>
<body style="font: 10pt verdana">
  <h4>Using a Simple Math Service </h4>
  <form runat="server"> 
  <div style="padding:15,15,15,15;background-color:beige;width:300;border-color:black;border-width:1;border-style:solid">
    Operand 1: <br><asp:TextBox id="Operand1" Text="15" runat="server"/><br>
    Operand 2: <br><asp:TextBox id="Operand2" Text="5" runat="server"/><p>
    <input type="submit" id="Add" value="Add" OnServerClick="Submit_Click" runat="server">
    <input type="submit" id="Subtract" value="Subtract" OnServerClick="Submit_Click" runat="server">
    <input type="submit" id="Multiply" value="Multiply" OnServerClick="Submit_Click" runat="server">
    <input type="submit" id="Divide" value="Divide" OnServerClick="Submit_Click" runat="server">
    <p>
    <asp:Label id="Result" runat="server"/>
  </div>
  </form>
</body>
</html>
我们还需要一个sdl文件,当然这个文件不用手工输入,我们在浏览一个.asmx的时候,在后缀名后直接加上?sdl可以自动生成sdl文件。
然后我们在一个.vb文件里,将定义一个名字空间,.vb文件的内容:
Imports System.Xml.Serialization
Imports System.Web.Services.Protocols
Imports System.Web.Services

Namespace MathServiceSpace
    Public Class  MathService
        Inherits System.Web.Services.Protocols.SoapClientProtocol
       
        Public Sub New()
            MyBase.New
            Me.Url = "http://localhost/QuickStart/aspplus/samples/services/MathService/VB/MathService.a"& _
"smx"
        End Sub
       Public Function <System.Web.Services.Protocols.SoapMethodAttribute("http://tempuri.org/Add")> Add(ByVal <System.Xml.Serialization.XmlElementAttribute("A", IsNullable:=false)> a As Integer, ByVal <System.Xml.Serialization.XmlElementAttribute("B", IsNullable:=false)> b As Integer) As Integer
            Dim results() As Object = Me.Invoke("Add", New Object() {a, b})
            Return CType(results(0),Integer)
        End Function
        Public Function BeginAdd(ByVal a As Integer, ByVal b As Integer, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult
            Return Me.BeginInvoke("Add", New Object() {a, b}, callback, asyncState)
        End Function
        Public Function EndAdd(ByVal asyncResult As System.IAsyncResult) As Integer
            Dim results() As Object = Me.EndInvoke(asyncResult)
            Return CType(results(0),Integer)
        End Function
        Public Function <System.Web.Services.Protocols.SoapMethodAttribute("http://tempuri.org/Subtract")> Subtract(ByVal <System.Xml.Serialization.XmlElementAttribute("A", IsNullable:=false)> a As Integer, ByVal <System.Xml.Serialization.XmlElementAttribute("B", IsNullable:=false)> b As Integer) As Integer
            Dim results() As Object = Me.Invoke("Subtract", New Object() {a, b})
            Return CType(results(0),Integer)
        End Function
        Public Function BeginSubtract(ByVal a As Integer, ByVal b As Integer, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult
            Return Me.BeginInvoke("Subtract", New Object() {a, b}, callback, asyncState)
        End Function
        Public Function EndSubtract(ByVal asyncResult As System.IAsyncResult) As Integer
            Dim results() As Object = Me.EndInvoke(asyncResult)
            Return CType(results(0),Integer)
        End Function
        Public Function <System.Web.Services.Protocols.SoapMethodAttribute("http://tempuri.org/Multiply")> Multiply(ByVal <System.Xml.Serialization.XmlElementAttribute("A", IsNullable:=false)> a As Integer, ByVal <System.Xml.Serialization.XmlElementAttribute("B", IsNullable:=false)> b As Integer) As Integer
            Dim results() As Object = Me.Invoke("Multiply", New Object() {a, b})
            Return CType(results(0),Integer)
        End Function
        Public Function BeginMultiply(ByVal a As Integer, ByVal b As Integer, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult
            Return Me.BeginInvoke("Multiply", New Object() {a, b}, callback, asyncState)
        End Function
        Public Function EndMultiply(ByVal asyncResult As System.IAsyncResult) As Integer
            Dim results() As Object = Me.EndInvoke(asyncResult)
            Return CType(results(0),Integer)
        End Function
        Public Function <System.Web.Services.Protocols.SoapMethodAttribute("http://tempuri.org/Divide")> Divide(ByVal <System.Xml.Serialization.XmlElementAttribute("A", IsNullable:=false)> a As Integer, ByVal <System.Xml.Serialization.XmlElementAttribute("B", IsNullable:=false)> b As Integer) As Integer
            Dim results() As Object = Me.Invoke("Divide", New Object() {a, b})
            Return CType(results(0),Integer)
        End Function
        Public Function BeginDivide(ByVal a As Integer, ByVal b As Integer, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult
            Return Me.BeginInvoke("Divide", New Object() {a, b}, callback, asyncState)
        End Function
        Public Function EndDivide(ByVal asyncResult As System.IAsyncResult) As Integer
            Dim results() As Object = Me.EndInvoke(asyncResult)
            Return CType(results(0),Integer)
        End Function
       
    End Class
End Namespace
有了这四个文件,我们可以编辑一个批处理文件,执行如下的语句:
    webserviceutil -c:proxy /pa:MathService.sdl /l:VB /n:MathServiceSpace
这样,我们就可以在客户端执行了。

5.2.1 小结
在这一章中,我们以提供计算加、减、乘、除的网络应用为例,详细的介绍了如何建立起一个完整的web service服务的步骤和注意事项,虽然这个例子和实际使用的应用环境有较大的差异,但基本方法应该是一致的。
第三章 数据交换
 我们的这个例子说明了DataSet-----一个基于XML技术的强大的数据分离技术,能够用Web Service方法返回。DataSet能够在一个智能化的结构中存储复杂的信息和关系,这是Web Service的一个非常有用的方法。
 
 通过DataSets的显示,你能够限制通过连接你的数据库服务器的测试。
 
 GetTitleAuthors方法连接一个数据库并且运行两个SQL语句,第一个返回颜色的列表,另外一个返回字体大小的列表。方法把两个结果用一个DataSet来存储,并返回一个DataSet。
 
 PutTitleAuthors说明一个Web Service方法把DataSet当作一个参数并返回一个整数,这个整数就是在DataSet中的“Table“表的行数。虽然这个方法执行起来有点简单,但是这个方法也能够与数据库服务器把过剩的数据聪明的合并在一起。
 
 我们来看看这个例子,首先:
 
 <%@ WebService Language="VB" Class="DataService" %>
 
这句话应该包括。我们还要引入这个名字空间:
 
 Imports System.Web.Services
 
第一我们两个方法,Getcolor():
 
    Public Function <WebMethod()> Getcolor() As DataSet
        Dim MyConnection As SQLConnection = New
SQLConnection("server=localhost;uid=sa;pwd=;database=howff")
        Dim MyCommand1 As SQLDataSetCommand = New
SQLDataSetCommand("select * from color", MyConnection)
        Dim MyCommand2 As SQLDataSetCommand = New
SQLDataSetCommand("select * from size", MyConnection)
  '数据的填充
        Dim DS As New DataSet
        MyCommand1.FillDataSet(DS, "color")
        MyCommand2.FillDataSet(DS, "size")
        Return DS
   End Function
 
Putcolor()方法:
 Public Function <WebMethod()> Putcolor(DS As DataSet) As Integer
        '返回行数 
        Return DS.Tables(0).Rows.Count
   End Function

文件保存为webservice.asmx,放在虚拟目录下,具体代码如下:
源文件:webservice\webservice.asmx

<%@ WebService Language="VB" Class="DataService" %>

Imports System
Imports System.Data
Imports System.Data.SQL

'引入System.Web.Services名字空间
Imports System.Web.Services

Public Class DataService

   Public Function <WebMethod()> Getcolor() As DataSet
 '创建数据库连接和命令集
        Dim MyConnection As SQLConnection = New
SQLConnection("server=localhost;uid=sa;pwd=;database=howff")
        Dim MyCommand1 As SQLDataSetCommand = New
SQLDataSetCommand("select * from color", MyConnection)
        Dim MyCommand2 As SQLDataSetCommand = New
SQLDataSetCommand("select * from size", MyConnection)
 '数据的填充
        Dim DS As New DataSet
        MyCommand1.FillDataSet(DS, "color")
        MyCommand2.FillDataSet(DS, "size")
        Return DS
   End Function
   '
   Public Function <WebMethod()> Putcolor(DS As DataSet) As Integer
        '返回行数 
        Return DS.Tables(0).Rows.Count
   End Function

End Class

一个简单的Web Service案例

[ 1 ]
一个简单的Web Service案例 num

打印本页 关闭

关于我们版权声明本站导航友情连结作品演示 TOP↑