今天我们来看一下如何在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) 我们选择"
Web services in this solution ",
c) 再选择我们刚刚创建的Customers服务。
d) 然后在Web reference Name中我们给我们的服务取个有意义的名字为:WS_Customers。

这些做好以后,我们可以观察在Web.config文件中自动生成了一些东西,如下:
如何创建一个WebService_数据

<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代码如下:
如何创建一个WebService_xml_02如何创建一个WebService_asp.net_03Code
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列出来了。


技术改变世界