博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何创建一个WebService
阅读量:7120 次
发布时间:2019-06-28

本文共 4888 字,大约阅读时间需要 16 分钟。

今天我们来看一下如何在VS2008中创建并应用一个基本的WebService。
场景:利用VS2008建立一个WebService,改服务取得Northwind下的 Customers表格数据。
        ASPX页面调用该服务,并将结果以GridView的形式显示在界面上。
首先,我们当然是做一个service了。
我们通过菜单生成一个Web Service,命名为:Customers,它的完整名字是:Customers.asmx。
代码如下:
using
 System;
using
 System.Collections;
using
 System.ComponentModel;
using
 System.Data;
using
 System.Linq;
using
 System.Web;
using
 System.Web.Services;
using
 System.Web.Services.Protocols;
using
 System.Xml.Linq;
using
 System.Data.SqlClient;
namespace
 BlogNet.WebService
{
    [WebService(Namespace 
=
 
"
http://www.cnblogs.com/davidgu/customers
"
)]
    [WebServiceBinding(ConformsTo 
=
 WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(
false
)]
    
//
 To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    
//
 [System.Web.Script.Services.ScriptService]
    
public
 
class
 Customers : System.Web.Services.WebService
    {
        [WebMethod]
        
public
 DataSet GetCustomers()
        {
            SqlConnection conn;
            SqlDataAdapter myDataAdapter;
            DataSet myDataSet;
            
string
 cmdString 
=
 
"
Select * From Customers
"
;
            conn 
=
 
new
 SqlConnection(
"
Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True
"
);
            myDataAdapter 
=
 
new
 SqlDataAdapter(cmdString, conn);
            myDataSet 
=
 
new
 DataSet();
            myDataAdapter.Fill(myDataSet, 
"
Customers
"
);
            
return
 myDataSet;
        }
    }
}
然后,我们需要在我们的solution中引用该服务。
鼠标点击solution,
a) 右键菜单->Add Web Reference,在弹出的对话框中,
b) 我们选择"
",
c) 再选择我们刚刚创建的Customers服务。
d) 然后在Web reference Name中我们给我们的服务取个有意义的名字为:WS_Customers。
这些做好以后,我们可以观察在Web.config文件中自动生成了一些东西,如下:
<
applicationSettings
>
  
<
BlogNet.Properties.Settings
>
   
<
setting 
name
="BlogNet_WS_Customers_Customers"
 serializeAs
="String"
>
    
<
value
>
http://localhost:1408/WebService/Customers.asmx
</
value
>
   
</
setting
>
  
</
BlogNet.Properties.Settings
>
 
</
applicationSettings
>
</
configuration
>
最后,我们写一下客户端页面如下:
<%
@ Page Language
=
"
C#
"
 AutoEventWireup
=
"
true
"
 CodeBehind
=
"
testCustomer.aspx.cs
"
 Inherits
=
"
BlogNet.WebService.testCustomer
"
 
%>
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html 
xmlns
="http://www.w3.org/1999/xhtml"
 
>
<
head 
runat
="server"
>
    
<
title
>
ASP.NET - 建立WebService
</
title
>
</
head
>
<
body
>
    
<
form 
id
="form1"
 runat
="server"
>
    
<
div
>
    
    
<
asp:GridView 
ID
="GridView1"
 
        runat
="server"
 
        AutoGenerateColumns
="False"
        AllowPaging
="True"
 
        AllowSorting
="True"
 
        PageSize
="20"
 
        OnPageIndexChanging
="GridView1_PageIndexChanging"
>
        
<
Columns
>
            
<
asp:BoundField 
DataField
="CustomerID"
 HeaderText
="CustomerID"
 ReadOnly
="True"
 
                SortExpression
="CustomerID"
 NullDisplayText
="N/A"
 
/>
            
<
asp:BoundField 
DataField
="CompanyName"
 HeaderText
="CompanyName"
 
                SortExpression
="CompanyName"
 NullDisplayText
="N/A"
 
/>
            
<
asp:BoundField 
DataField
="ContactName"
 HeaderText
="ContactName"
 
                SortExpression
="ContactName"
 NullDisplayText
="N/A"
 
/>
            
<
asp:BoundField 
DataField
="ContactTitle"
 HeaderText
="ContactTitle"
 
                SortExpression
="ContactTitle"
 NullDisplayText
="N/A"
 
/>
            
<
asp:BoundField 
DataField
="Address"
 HeaderText
="Address"
 
                SortExpression
="Address"
 NullDisplayText
="N/A"
 
/>
            
<
asp:BoundField 
DataField
="City"
 HeaderText
="City"
 SortExpression
="City"
 NullDisplayText
="N/A"
 
/>
            
<
asp:BoundField 
DataField
="Region"
 HeaderText
="Region"
 
                SortExpression
="Region"
 NullDisplayText
="N/A"
 
/>
            
<
asp:BoundField 
DataField
="PostalCode"
 HeaderText
="PostalCode"
 
                SortExpression
="PostalCode"
 NullDisplayText
="N/A"
 
/>
            
<
asp:BoundField 
DataField
="Country"
 HeaderText
="Country"
 
                SortExpression
="Country"
 NullDisplayText
="N/A"
 
/>
            
<
asp:BoundField 
DataField
="Phone"
 HeaderText
="Phone"
 SortExpression
="Phone"
 NullDisplayText
="N/A"
 
/>
            
<
asp:BoundField 
DataField
="Fax"
 HeaderText
="Fax"
 SortExpression
="Fax"
 NullDisplayText
="N/A"
 
/>
        
</
Columns
>
    
</
asp:GridView
>
    
    
</
div
>
    
</
form
>
</
body
>
</
html
>
CS代码如下:
ContractedBlock.gif
ExpandedBlockStart.gif
Code
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace BlogNet.WebService
{
    [WebService(Namespace 
= "http://www.cnblogs.com/davidgu/customers")]
    [WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(
false)]
    
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    
// [System.Web.Script.Services.ScriptService]
    public class Customers : System.Web.Services.WebService
    {
        [WebMethod]
        
public DataSet GetCustomers()
        {
            SqlConnection conn;
            SqlDataAdapter myDataAdapter;
            DataSet myDataSet;
            
string cmdString = "Select * From Customers";
            conn 
= new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=True");
            myDataAdapter 
= new SqlDataAdapter(cmdString, conn);
            myDataSet 
= new DataSet();
            myDataAdapter.Fill(myDataSet, 
"Customers");
            
return myDataSet;
        }
    }
}
运行下程序,网页上便会以分页形式把Customers表的数据全部以GridView列出来了。
你可能感兴趣的文章
最课程学员启示录:一份有诚意的检讨书
查看>>
即时通信(IM)和实时通信(RTC)的区别
查看>>
面试题解:输入一个数A,找到大于A的一个最小数B,且B中不存在连续相等的两个数字...
查看>>
Linux Linux程序练习九
查看>>
Nginx的启动、停止与重启
查看>>
Windows 64 位 mysql 5.7.20 安装教程
查看>>
css点滴3—5种方式实现圆环
查看>>
剑指offer 最小的k个数 leetcode 215. Kth Largest Element in an Array
查看>>
screen 命令使用及示例
查看>>
windows10环境下的RabbitMQ安装步骤(图文)
查看>>
Kafka:ZK+Kafka+Spark Streaming集群环境搭建(二十八):kafka0.10.1 内置性能测试API用法示例...
查看>>
【html+css3】在一张jpg图片上,显示多张透明的png图片
查看>>
WPF 3D模型的一个扩展方法
查看>>
postgreSQL学习(二):pgsql的一些基础操作
查看>>
openstack之镜像管理
查看>>
Gartner 2018 年WAF魔力象限报告:云WAF持续增长,Bot管理与API安全拥有未来
查看>>
如何在Java客户端调用RESTful服务
查看>>
一个月薪两万的Web安全工程师要掌握哪些技能?
查看>>
同事写得Python对页面压测脚本
查看>>
H2:开源内存数据库引擎
查看>>