• 好长时间没上了,呵呵!最近在学习Web Service,顺便写个简单的体会吧


    首先创建Web Service 项目
    主要代码如下:
    在appcode 里的service.vb 里添加方法:
    'HelloWorld 由系统自动生成。
    '我添加了两个方法
    'addservice 实现简单的两个数的加法运算,
    'myds 生成一个简单的数据集
    '为后边我们调用做准备。
    '注意:如果你想要在其他地方使用web service 上的方法和数据的话,
    '在方法定义的前面必须加上 “<WebMethod()> _”
    Imports System.Web
    Imports System.Web.Services
    Imports System.Web.Services.Protocols
    Imports System.Data

    <WebService(Namespace:="http://tempuri.org/")> _
    <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    Public Class Service
         Inherits System.Web.Services.WebService

        <WebMethod()> _
        Public Function HelloWorld() As String
            Return "Hello World"
        End Function
        <WebMethod()> _
           Public Function AddService(ByVal a As Integer, ByVal b As Integer) As String
            Return a + b
        End Function

        <WebMethod()> _
        Public Function Myds() As DataSet
            Dim ds As New DataSet
            Dim dt As New DataTable("test")
            Dim col1 As DataColumn = dt.Columns.Add("fname", System.Type.GetType("System.String"))
            Dim col2 As DataColumn = dt.Columns.Add("fsex", System.Type.GetType("System.String"))
            Dim dr As DataRow
            For i As Integer = 0 To 5
                dr = dt.NewRow
                dr(0) = "wang" & i
                dr(1) = "man"
                dt.Rows.Add(dr)
            Next
            ds.Tables.Add(dt)
            Return ds
        End Function
    End Class

    在本地生成http://localhost/WebSite1/Service.asmx 服务地址

    接着我们在创建一个简单的网站项目
    因为我们做的是web service测试,所以我们需要添加对它的引用,
    将服务地址 http://localhost/WebSite1/Service.asmx 添加到里面,取引用名为localhost
    在页面里我们添加一个datagridview控件
    后台代码如下:
    Imports localhost.Service
    Partial Class _Default
        Inherits System.Web.UI.Page
        Dim a As localhost.Service
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            a = New localhost.Service '实例话该服务
            Me.dgv.DataSource = a.Myds.Tables(0) '调用服务里的数据集
            Me.dgv.DataBind()
        End Sub
    End Class
    运行该网站,我们便可以看到从服务获取到的数据。
    接着我们创建一个windows项目,来实现对服务的使用
    方法如同网站的操作,自己可以按照上面的步骤来实现对服务的使用。我就略了,呵呵!
    这是最简单的应用,复杂的我们一起学习!



  • 相关阅读:
    增量+全量备份SVN服务器
    日常小命令集锦
    filebeat输出到kafka
    在Logstash的配置文件中对日志事件进行区分
    NFS服务器简易安装
    记录一次MySQL数据库CPU负载异常高的问题
    使用Spring的jdbcTemplate进一步简化JDBC操作
    Stream 和 byte[] 之间的转换
    C# 文件转byte数组,byte数组再转换文件
    groupbox里面添加Form
  • 原文地址:https://www.cnblogs.com/chaodongwang/p/1016469.html
Copyright © 2020-2023  润新知