第5章 Web Service

Web Service的概述:要给第三方暴露我们数据和方法的一种方式.
Xml是Web Service的基础,Web Service有几个协议并且遵守它们如:SOAP,WSDL,UDDI.
简单对象访问协议(SOAP)是Web service最重要的协议.
Web Service中用[WebMethod]特性表示一个web方法.
一个Web Service文件中可以有多个[WebMethod],且一个[WebMethod]只能对它下面一个方法有效.

如下一个示例,调用Web Service中的方法:
WebService中的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace practice
{
    /// <summary>
    /// web 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    [System.Web.Script.Services.ScriptService]
    public class web : System.Web.Services.WebService
    {

        [WebMethod]
        public string[] HelloWorld(string prefix, int count)
        {
            return "Hello Word";
        }

        [WebMethod]
        public string[] Show(string prefix, int count)
        {
            Random rand = new Random();
            List<string> item = new List<string>(count);
            for (int i = 0; i < count; i++)
            {
                char c1 = (char)rand.Next(65, 90);
                char c2 = (char)rand.Next(100, 122);
                char c3 = (char)rand.Next(130, 150);
                item.Add(prefix + c1 + c2 + c3);
            }
            return item.ToArray();
        }
    }
}
View Code

页面代码如下:(需要引用AjaxControlToolkit.dll文件)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="practice.WebForm1" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    </asp:ToolkitScriptManager>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" ServiceMethod="Show" ServicePath="../web.asmx" TargetControlID="TextBox1" MinimumPrefixLength="1">
    </asp:AutoCompleteExtender>
    </form>
</body>
</html>

 

posted @ 2013-12-22 12:17  mmww  阅读(122)  评论(0)    收藏  举报